标签:des io sp java for on html amp as
5
#include<stdio.h> #include<algorithm> using namespace std; struct data { int a,b,dist; }w[11000]; int father[110] ; int cmp(data x,data y) { return x.dist<y.dist; } int find(int x) { if(x==father[x]) return x; return find(father[x]); } int main() { int N; while(scanf("%d",&N),N) { int m,i,max=0; m=N*(N-1)/2; for(i=1;i<=m;++i) { scanf("%d%d%d",&w[i].a,&w[i].b,&w[i].dist); } sort(w+1,w+m+1,cmp); for(i=1;i<=N;++i) father[i]=i; for(i=1;i<=m;++i) { int x=w[i].a,y=w[i].b; x=find(x);y=find(y); if(x!=y) { father[x]=y; //father[y]=father[x]; max+=w[i].dist; } } printf("%d\n",max); } return 0; }
标签:des io sp java for on html amp as
原文地址:http://blog.csdn.net/qq_18062811/article/details/41130081