标签:
Description
Input
Output
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
Source
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int inf = 0x3f3f3f3f,MAX = 3000; int map[MAX][MAX], k[MAX]; void prim (int map[MAX][MAX],int n){ int min1,m,ans = 0; for(int i = 1; i <= n ;i++) k[i] = map[1][i]; for(int i = 1; i < n ;i++){ int min1 = inf; m = 0; for(int j = 1; j <= n ;j++){ if(k[j] < min1 && k[j]!=0){ min1 = k[j]; m = j; } } k[m] = 0; ans +=min1; for(int j = 1; j <= n ; j++){ if(k[j] > map[m][j] && k[j]!=0) k[j] = map[m][j]; } } printf("%d\n",ans); } int main() { int n,a; while(~scanf("%d",&n)&&n){ for(int i = 1; i <= n ;i++) for(int j = 1; j <= n ;j++) scanf("%d",&map[i][j]); prim(map,n); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4321770.html