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<iostream> #include<sstream> #include<algorithm> #include<cstdio> #include<string.h> #include<cctype> #include<string> #include<cmath> #include<vector> #include<stack> #include<queue> #include<map> #include<set> using namespace std; const int INF = 101; int F[INF]; int n,ans; struct edges { int u,v,w; } per[INF*INF]; bool cmp(edges a,edges b) { return a.w<b.w; } void init() { for(int i = 1; i <= n; i++) { F[i] = i; } } int Find(int x) { return x==F[x]? x:Find(F[x]); } int main() { while(cin>>n,n) { ans=0; int m = n*(n-1)/2; for(int i = 0; i<m; i++) scanf("%d%d%d",&per[i].u,&per[i].v,&per[i].w); sort(per,per + m,cmp); init(); int count = 0; for(int i=0; i<m; i++) { int tx=Find(per[i].u); int ty=Find(per[i].v); if(tx != ty) { count++; F[tx]=ty; ans+=per[i].w; if(count==n-1) break; } } cout<<ans<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lsgqjh/article/details/47024233