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