标签:des blog http io ar os sp java for
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17984 Accepted Submission(s): 7616
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> using namespace std; #define N 105 int n,m,set[N],ans,k; 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,k=0; scanf("%d",&m); for(int i=1;i<=m;i++) set[i]=i; for(int i=1;i<=n;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,cmp); for(int i=1;i<=n;i++) { int ux,uy; ux=find(e[i].x); uy=find(e[i].y); if(ux!=uy) { set[ux]=uy; ans+=e[i].z; } } for(int i=1;i<=m;i++) if(set[i]==i) k++; if(k>1) printf("?\n"); else printf("%d\n",ans); } return 0; }
标签:des blog http io ar os sp java for
原文地址:http://www.cnblogs.com/a972290869/p/4101109.html