标签:
Description
Input
Output
Sample Input
Sample Output
Hint
Hint Huge input, scanf is recommended.
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #define maxn 0x3fff 5 using namespace std; 6 int d[110] , map[110][110] , v[110] ;//d[i]用来存放(i - 1) - th ~ i - th (point) 的权值 , v[i] 中0表示没选过 , 1表示选过 7 int n , m ; 8 9 int prim (int start ) 10 { 11 int mm , x ; 12 for (int i = 1 ; i <= n ; i++) 13 d[i] = i == start ? 0 : maxn ; //给 起始点 和 非起始点 涂色 14 for (int i = 1 ; i <= n ; i++) { 15 mm = maxn ; 16 for (int j = 1 ; j <= n ; j++) { //找出权值最小的边 17 if ( !v[j] && d[j] < mm ) { 18 mm = d[j] ; 19 x = j ; 20 } 21 } 22 v[x] = 1 ; 23 for (int j = 1 ; j <= n ; j++ ) { 24 if ( !v[j] && d[j] > map[x][j] && map[x][j] != maxn ) 25 d[j] = map[x][j] ; 26 } 27 } 28 int tmp = 0 ; 29 for (int i = 1 ; i <= n ; i++) 30 tmp += d[i] ; 31 return tmp ; 32 } 33 34 int main () 35 { 36 // freopen ("a.txt" , "r" , stdin ) ; 37 int val ; 38 int a , b ; 39 int res ; 40 while ( cin >> n , n) { 41 m = n * (n - 1) >> 1 ; 42 memset (map , 0x3f , sizeof(map) ) ; 43 memset (v , 0 , sizeof(v) ) ; 44 for(int i = 1 ; i <= m ; i++ ) 45 { 46 scanf ("%d%d%d", &a , &b , &val ); 47 if( val < map[a][b] ) 48 { 49 map[a][b] = val; 50 map[b][a] = val; 51 } 52 } 53 res = prim(1) ; 54 cout << res << endl ; 55 } 56 return 0 ; 57 }
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4281900.html