码迷,mamicode.com
首页 > Web开发 > 详细

POJ 1258 Agri-Net

时间:2015-01-22 17:18:21      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

 

/*
*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;
}

  

POJ 1258 Agri-Net

标签:

原文地址:http://www.cnblogs.com/subrshk/p/4241705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!