标签:
/* *POJ 1258 Agri-Net *求最小生成树权值和 */ #include <cstdio> #include <cstring> #define INF 1000000 #define MAXN 101 int mark, n; int map[MAXN][MAXN]; int lowcost[MAXN]; int prime() { int i, j; int sum = 0; mark = 0; lowcost[0] = -1; for (i = 1; i < n; i++) { lowcost[i] = map[0][i]; } for (i = 1; i < n ; i++) { int min = INF; for (j = 0; j < n; j++) { if (lowcost[j] != -1 && lowcost[j] < min) { min = lowcost[j]; mark = j; } } sum += min; lowcost[mark] = -1; for (j = 0; j < n; j++) { if (map[mark][j] < lowcost[j]) { lowcost[j] = map[mark][j]; } } } return sum; } int main() { int i, j; while (~scanf("%d", &n)) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &map[i][j]); } } printf("%d\n", prime()); } return 0; }
标签:
原文地址:http://www.cnblogs.com/subrshk/p/4241705.html