标签:目标 i++ while ring string case 用例 str ini
1 /* 2 Time: 2017/8/8 3 Author: WTZPT 4 */ 5 #include<iostream> 6 #include<cstring> 7 #include<algorithm> 8 using namespace std; 9 int parent[105]; 10 11 struct node{ //定义一个结构体,方便数据收集 12 int a,b,s; //a,b是两个村庄的编号 s是两个村庄的距离 13 }caseii[5005]; 14 15 void init() //初始化parent数组 16 { 17 for(int i = 0; i < 105; i++) 18 parent[i] = i; 19 } 20 21 bool cmp(node a,node b) //定义sort排序方式 22 { 23 return a.s < b.s; 24 } 25 26 int find(int n) //找根值 27 { 28 if(parent[n] == n) 29 return n; 30 else 31 return find(parent[n]); 32 } 33 34 int merge(int a,int b) 35 { 36 a = find(a); 37 b = find(b); 38 if(a != b) 39 { 40 parent[b] = a; 41 return 1; 42 } 43 return 0; 44 } 45 46 int main() 47 { 48 int n; 49 while(cin>>n&&n) 50 { 51 init(); 52 int t = n*(n-1)/2; 53 for(int i =0; i < t;i++) 54 cin>>caseii[i].a>>caseii[i].b>>caseii[i].s; 55 sort(caseii,caseii+t,cmp); 56 int sum = 0; 57 for(int i = 0; i < t;i++) 58 { 59 int temp = merge(caseii[i].a,caseii[i].b); 60 if(temp) sum += caseii[i].s; 61 } 62 63 cout<<sum<<endl; 64 } 65 66 return 0; 67 }
标签:目标 i++ while ring string case 用例 str ini
原文地址:http://www.cnblogs.com/jj81/p/7305089.html