poj1125

floyd算法,floyd过后查看每个点传播消息需要的时间,把最小的点输出即可。

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int maxn = 101;

int dist[maxn][maxn];
int n;

void init()
{
	memset(dist, -1, sizeof(dist));
	for (int i = 0; i < n; i++)
	{
		int m;
		scanf("%d", &m);
		for (int j = 0; j < m; j++)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			a--;
			dist[i][a] = b;
		}
	}
}

void floyd()
{
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (dist[j][i] != -1)
				for (int k = 0; k < n; k++)
					if (dist[i][k] != -1)
						if (dist[j][k] == -1 || dist[j][k] > dist[j][i] + dist[i][k])
							dist[j][k] = dist[j][i] + dist[i][k];
}

void work()
{
	int ans = 1000000000, ansi = -1;

	for (int i = 0; i < n; i++)
	{
		bool ok = true;
		int maxdist = 0;
		for (int j = 0; j < n; j++)
		{
			if (i == j)
				continue;
			if (dist[i][j] == -1)
			{
				ok = false;
				break;
			}
			if (maxdist < dist[i][j])
				maxdist = dist[i][j];
		}
		if (maxdist < ans && ok)
		{
			ans = maxdist;
			ansi = i;
		}
	}
	if (ansi == -1)
		printf("disjoint\n");
	else
		printf("%d %d\n", ansi + 1, ans);
}

int main()
{
	//freopen("D:\\t.txt", "r", stdin);
	while (scanf("%d", &n) != EOF && n != 0)
	{
		init();
		floyd();
		work();
	}
	return 0;
}
posted @ 2011-02-01 14:14  金海峰  阅读(747)  评论(0编辑  收藏  举报