84分求助
查看原帖
84分求助
521283
wangif424楼主2022/11/19 21:34
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
struct b{
	int x,y,n;
}v[200100];
int f[5001],n,m,ans; 
bool cmp(b x,b y){
	return x.n<y.n;
}
int find(int x){
	if(f[x]==x)return x;
	else return f[x]=find(f[x]);
}
signed main(){
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		f[i]=i;
	}
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin >> x >> y >> z;
		v[i].n=z;
		v[i].x=x;
		v[i].y=y;
	}
	sort(v+1,v+1+m,cmp);
	for(int i=1;i<=m;i++){
		int vx=find(v[i].x),vy=find(v[i].y);
		if(vx==vy)continue;
		ans+=v[i].n;
		f[vx]=vy;
	}
	cout << ans;
	return 0;
}

#13WA了

2022/11/19 21:34
加载中...