标签:style blog http color strong os
1 4 6 1 2 10 2 3 10 3 1 10 1 4 1 2 4 1 3 4 1 1 3 5 6
4
解题:最小生成树算法,我用kruskal算法做的。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 using namespace std; 11 struct ARC{ 12 int x,y,c; 13 }; 14 ARC e[130000]; 15 int d[510],uf[510]; 16 bool cmp(const ARC &a,const ARC &b){ 17 return a.c < b.c; 18 } 19 int findPa(int x){ 20 if(x != uf[x]) uf[x] = findPa(uf[x]); 21 return uf[x]; 22 } 23 int main(){ 24 int k,i,j,n,v,theMin,temp,ans; 25 scanf("%d",&k); 26 while(k--){ 27 scanf("%d %d",&n,&v); 28 for(i = 0; i < v; i++) scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].c); 29 sort(e,e+v,cmp); 30 theMin = INT_MAX>>2; 31 for(i = 0; i < n; i++){ 32 scanf("%d",&temp); 33 if(temp < theMin) theMin = temp; 34 } 35 for(i = 0; i < 510; i++) uf[i] = i; 36 for(ans = i = 0; i < v; i++){ 37 int x = findPa(e[i].x); 38 int y = findPa(e[i].y); 39 if(x != y){ 40 ans += e[i].c; 41 uf[x] = y; 42 } 43 } 44 printf("%d\n",theMin+ans); 45 } 46 return 0; 47 }
标签:style blog http color strong os
原文地址:http://www.cnblogs.com/crackpotisback/p/3855395.html