标签:
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
3 5Huge input, scanf is recommended.HintHint
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define maxn 100001 int father[maxn]; int flag; void init() { for(int i=0;i<maxn;i++) father[i]=i; } int Find(int x) { return x==father[x]?x:father[x]=Find(father[x]); } void Union(int x,int y) { int fx=Find(x); int fy=Find(y); if(fx==fy) { flag=0; } else { father[fy]=fx; flag=1; } } struct city { int a,b,c; }; int cmp(city x,city y) { return x.c<y.c; } int main() { int m,n,q; struct city h[10001]; while(cin>>m) { if(!m) break; q=0; init(); n=m*(m-1)/2; for(int i=0;i<n;i++) { cin>>h[i].a>>h[i].b>>h[i].c; } sort(h,h+n,cmp); for(int i=0;i<n;i++) { Union(h[i].a,h[i].b); if(flag) q+=h[i].c; } cout<<q<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/novak_djokovic/article/details/44816443