#include<iostream>
#include<fstream>
using namespace std;
static const int MAXNODES = 101;
static const int MAX = 999; /* caution */
static int weigh[MAXNODES][MAXNODES];
static int broker, shortest;
static int n;
//#define DEBUG
/* 256K 16MS */
void floyd_warshall()
{
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (weigh[i][k] + weigh[k][j] < weigh[i][j])
weigh[i][j] = weigh[i][k] + weigh[k][j];
int max; shortest = MAX;
for (i = 1; i <= n; i++)
{
max = 0;
for (j = 1; j <= n; j++)
{
if (j != i && weigh[i][j] > max) /* caution */
max = weigh[i][j];
}
if (max < shortest)
{ broker = i; shortest = max; }
}
}
int main()
{
#ifdef DEBUG
fstream cin("G:\\book\\algorithms\\acm\\Debug\\dat.txt");
#endif
while (cin >> n && n != 0)
{
int i, j, t;
int c;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
weigh[i][j] = MAX;
for (i = 1; i <= n; i++)
{
cin >> c;
while (c-- > 0)
{
cin >> j >> t;
weigh[i][j] = t;
}
}
floyd_warshall();
cout << broker << " " << shortest << "\n";
}
return 0;
}
n 个传播源, 一个传播路径有向图。
从那个传播源开始传播,消息传播的最快? 是每对顶点间最短路径的应用。
floyd-warshall 的相关证明见 算法导论 25.2节。推导式子 25.5。
应该还可以优化。