标签:hdu1879
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13613    Accepted Submission(s): 5906
 
 
 
3 1 2 1 0 1 3 2 0 2 3 4 0 3 1 2 1 0 1 3 2 0 2 3 4 1 3 1 2 1 0 1 3 2 1 2 3 4 1 0
 
3 1 0
#include <stdio.h>
#include <string.h>
#define maxn 102
int map[maxn][maxn];
bool vis[maxn];
void Prim(int n)
{
	int i, j, count = 0, len = 0, tmp, u;
	vis[1] = 1;
	while(count < n - 1){
		for(i = 1, tmp = -1; i <= n; ++i){
			if(!vis[i]) continue;
			for(j = 1; j <= n; ++j)
				if(map[i][j] != -1 && !vis[j] && (tmp == -1 || map[i][j] < tmp)){
					tmp = map[i][j]; u = j;
				}
		}
		if(tmp != -1){
			++count; vis[u] = 1;
			len += tmp;
		}
	}
	printf("%d\n", len);
}
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int n, i, a, b, c, d, m;
	while(scanf("%d", &n), n){
		memset(map, -1, sizeof(map));
		memset(vis, 0, sizeof(vis));
		m = n * (n - 1) / 2;
		for(i = 0; i < m; ++i){
			scanf("%d%d%d%d", &a, &b, &c, &d);
			if(d) map[a][b] = map[b][a] = 0;
			else if(map[a][b] == -1 || c < map[a][b]){
				map[a][b] = map[b][a] = c;
			}
		}
		Prim(n);
	}
	return 0;
}
 
HDU1879 继续畅通工程 【最小生成树Prim】,布布扣,bubuko.com
标签:hdu1879
原文地址:http://blog.csdn.net/chang_mu/article/details/38260053