UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198983#2599. ysgh的车Zxc200611100137ms4208kbC++111.5kb2023-12-03 11:25:222023-12-03 12:22:49

answer

/*
直接跑费用流可以做到 O(M sqrt N) = O(n^4) 70pts。
模拟费用流?

在完全二分图上选一个基环树森林,使得每个点在一个基环树中。边有边权,求最小边权和。
好像是拟阵?直接贪心。
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct DisjointSetUnion
{
	struct Node
	{
		int fth,siz,ecnt;
	};
	Node t[210000];
	void init(int n)
	{
		for(int i=1;i<=n;i++)
			t[i]=(Node){i,1,0};
	}
	int find(int u)
	{
		return t[u].fth==u?u:(t[u].fth=find(t[u].fth));
	}
	bool addEdge(int u,int v)
	{
		u=find(u),v=find(v);
		if(u==v)
		{
			if(t[u].ecnt==t[u].siz)
				return 0;
			t[u].ecnt+=1;
			return 1;
		}
		if(t[u].siz<t[v].siz) // merge v to u
			swap(u,v);
		if(t[u].ecnt==t[u].siz&&t[v].ecnt==t[v].siz)
			return 0;
		t[v].fth=u,t[u].siz+=t[v].siz,t[u].ecnt+=t[v].ecnt+1;
		return 1;
	}
};
struct Edge
{
	int u,v,l;
};
int n,m;
vector<Edge> edg;
DisjointSetUnion dsu;
signed main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			int x;
			cin>>x;
			edg.push_back((Edge){i,n+j,x});
		}
	}
	sort(edg.begin(),edg.end(),[&](Edge a,Edge b){return a.l<b.l;});
	dsu.init(n+m);
	int ans=0;
	for(int i=0;i<edg.size();i++)
	{
		// cout<<"Add Edge ("<<edg[i].u<<","<<edg[i].v<<") l="<<edg[i].l<<endl;
		if(dsu.addEdge(edg[i].u,edg[i].v))
		{
			// cout<<"Success"<<endl;
			ans+=edg[i].l;
		}
	}
	cout<<ans<<endl;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 1ms
memory: 1256kb

input:

3 3
446652790 367197750 439301791
632719052 735727031 263827380
781425866 746814817 125123655

output:

2274822418

result:

ok single line: '2274822418'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1256kb

input:

5 5
704973224 223286523 185440833 391522443 71231270
755954965 323006835 3677105 890811159 500348652...

output:

1577388472

result:

ok single line: '1577388472'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1256kb

input:

5 5
706172801 219946739 682115835 439659203 654106361
702641914 786757740 486606000 893635276 864854...

output:

2045477736

result:

ok single line: '2045477736'

Test #4:

score: 10
Accepted
time: 0ms
memory: 1260kb

input:

5 20
424644761 401498122 978798479 727599263 249496176 791979610 484156315 427490341 881845882 96362...

output:

3918738749

result:

ok single line: '3918738749'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1260kb

input:

8 12
94906966 809163705 401492815 972035716 342151668 727945486 66475736 74081095 664139684 58772684...

output:

2376201054

result:

ok single line: '2376201054'

Test #6:

score: 10
Accepted
time: 2ms
memory: 1384kb

input:

50 50
708288313 116258534 999421377 331957777 737738408 605715051 263259419 71828794 215476805 21252...

output:

1090767607

result:

ok single line: '1090767607'

Test #7:

score: 10
Accepted
time: 1ms
memory: 1304kb

input:

30 30
709487889 112885982 422321787 380094537 246838907 552401999 727010324 481015865 218268154 8724...

output:

1126834645

result:

ok single line: '1126834645'

Test #8:

score: 10
Accepted
time: 36ms
memory: 4208kb

input:

20000 5
601961413 500964482 376759424 900291631 433034185
818335307 115252679 728477136 69394251 907...

output:

3335254892871

result:

ok single line: '3335254892871'

Test #9:

score: 10
Accepted
time: 53ms
memory: 4204kb

input:

300 333
483783421 608186772 359639944 743972978 257598049 783325645 426821531 566755525 676920766 85...

output:

2527786445

result:

ok single line: '2527786445'

Test #10:

score: 10
Accepted
time: 44ms
memory: 4204kb

input:

320 312
122459380 113245258 726089536 449890743 434479316 360590554 168991887 114573670 977139711 98...

output:

2438575800

result:

ok single line: '2438575800'

Extra Test:

score: 0
Extra Test Passed