- 团队
- 2018-09-12 20:43:35 @
无
2 条评论
-
ccc123 LV 5 @ 2018-09-12 20:52:09
#include<bits/stdc++.h>
using namespace std;
#define maxn 100000
struct edge{
int x,y,z;
};
edge a[maxn];
int m,n;
int f[maxn];
void makeSet(){
for(int i=1;i<=n;i++){
f[i]=i;
}
}
int find(int x){
if(f[x]==x)return x;
else {
f[x]=find(f[x]);
return f[x];}
}
void union_(int x,int y){
f[find(x)]=find(y);
}
bool cmp(edge x,edge y){
return x.z<y.z;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
}
sort(a+1,a+1+m,cmp);
makeSet();
for(int i=1;i<=m;i++){
if(find(a[i].x)!=find(a[i].y)){
union_(x,y);
ans+=f[i].z;
}
}
cout<<ans;
} -
2018-09-12 20:44:53@
无聊
- 1