标签:des blog http io ar os sp java for
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27000 Accepted Submission(s): 12042
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<cstdlib> #include<queue> #include<vector> using namespace std; #define N 110 int n,set[N],ans; struct line { int x; int y; int z; }e[N*N]; bool cmp(line a,line b) { return a.z<b.z; } int find(int x) { return x==set[x]?x:set[x]=find(set[x]); } int main() { while(scanf("%d",&n),n) { ans=0; for(int i=1;i<=n;i++) set[i]=i; for(int i=1;i<=n*(n-1)/2;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); e[i].x=x,e[i].y=y,e[i].z=z; } sort(e+1,e+1+n*(n-1)/2,cmp); for(int i=1;i<=n*(n-1)/2;i++) { int ux,uy; ux=find(e[i].x); uy=find(e[i].y); if(ux!=uy) { set[ux]=uy; ans+=e[i].z; } } printf("%d\n",ans);; } return 0; }
标签:des blog http io ar os sp java for
原文地址:http://www.cnblogs.com/a972290869/p/4101108.html