标签:des style blog http color os java io strong
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17593 Accepted Submission(s):
7417
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 int father[110],sum; 6 struct node 7 { 8 int s,d,f; 9 } p[110]; 10 11 bool cmp(const node &a,const node &b)//用来排序的,这里只需要给费用排序就可以了 12 { 13 return a.f<b.f; 14 } 15 16 void set(int n)//初始化,刚开始全部指向自己 17 { 18 for (int i=1; i<=n; i++) 19 father[i]=i; 20 } 21 22 int find(int a)//来查找是否属于同一个父节点,找最终的顶点 23 { 24 if (father[a]==a) 25 return a; 26 return father[a]=find(father[a]); 27 } 28 29 void Union(int x,int y,int z)//如果不是一个顶点的话,可以把这两个指向,连在一起 30 { 31 x=find(x); 32 y=find(y); 33 if (x!=y) 34 { 35 father[x]=y; 36 sum+=z; 37 } 38 } 39 40 int main() 41 { 42 int n,m,k; 43 while (cin>>n>>m) 44 { 45 if (n==0) 46 break; 47 sum=0,k=0; 48 set(m); 49 for (int i=1; i<=n; i++) 50 { 51 cin>>p[i].s>>p[i].d>>p[i].f; 52 } 53 sort(p,p+n,cmp); 54 for (int i=1; i<=n; i++) 55 { 56 Union(p[i].s,p[i].d,p[i].f); 57 } 58 for (int i=1; i<=m; i++)//这里就是来判断有几个地点,如果是一个顶点的话则成立 59 { 60 if (father[i]==i) 61 k++; 62 } 63 if (k>1) 64 printf ("?\n"); 65 else 66 printf ("%d\n",sum); 67 } 68 return 0; 69 }
标签:des style blog http color os java io strong
原文地址:http://www.cnblogs.com/qq-star/p/3936931.html