最小生成树

struct edge{
int u,v,value;
bool operator < (const edge &t){
return value<t.value;
}
}e[maxn];
int fath[maxn];
int find(int x){
return x == fath[x] ? x : fath[x] = find(fath[x]);
}
int Kruskal(){
int ans=0;
for(int i = 1; i <= n; i++)fath[i] = i;
sort(e+1,e+m+1);
for(int i = 1; i <= m; i++){
int fx = find(e[i].u);
int fy = find(e[i].v);
if(fx != fy){
fath[fy] = fx;
ans+=e[i].value;
}
}
return ans;
}
posted @ 2020-10-13 15:18  yesuweiYYYY  阅读(138)  评论(0编辑  收藏  举报