给出修建边的边权,求连通所有点的最小花费
最小生成树裸题
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; struct seg{
int a,b,l;
bool operator <(const seg a)const{
return l<a.l;
}
}s[]; int n,fa[]; void init(){
for(int i=;i<=n;i++)fa[i]=i;
} int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
} int main(){
while(scanf("%d",&n)!=EOF&&n!=){
int c=n*(n-)/;
int i;
for(i=;i<=c;i++){
scanf("%d%d%d",&s[i].a,&s[i].b,&s[i].l);
}
sort(s+,s+c+);
init();
int t=,ans=;
for(i=;i<=c;i++){
int x=find(s[i].a),y=find(s[i].b);
if(x!=y){
ans+=s[i].l;
t++;
fa[x]=y;
if(t==n-)break;
}
}
printf("%d\n",ans);
}
return ;
}