标签:des style blog http color java os io
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16937 Accepted Submission(s): 7099
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN=100+10; 7 struct node 8 { 9 int star; 10 int en; 11 int val; 12 bool operator <(const node &B)const 13 { 14 return val<B.val; 15 } 16 }a[MAXN]; 17 int p[MAXN]; 18 int Find(int x) 19 { 20 return p[x]==x?x:p[x]=Find(p[x]); 21 } 22 int main() 23 { 24 //freopen("in.txt","r",stdin); 25 int n,m; 26 while(scanf("%d %d",&n,&m)&&n) 27 { 28 int ans=0,cnt=0; 29 for(int i=1;i<=n;i++) 30 scanf("%d %d %d",&a[i].star,&a[i].en,&a[i].val); 31 sort(a+1,a+n+1); 32 for(int i=1;i<=m;i++) p[i]=i; 33 for(int i=1;i<=n;i++) 34 { 35 int x=Find(a[i].star); 36 int y=Find(a[i].en); 37 if(x!=y) 38 { 39 cnt++; 40 ans+=a[i].val; 41 if(cnt==m-1) 42 break; 43 p[x]=y; 44 } 45 } 46 if(cnt==m-1) 47 printf("%d\n",ans); 48 else 49 printf("?\n"); 50 } 51 return 0; 52 }
HDU 1863 畅通工程 (最小生成树),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/clliff/p/3902923.html