题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1125
设任意顶点vi,它向图中其它顶点传递信息的时间定义为,它到其它顶点的最短路径长度的最长者,该题要求vi中传递信息时间最短的时间。直接用Floyd即可。
#include <iostream>
using namespace std;
#define MAX 101
#define INF 10000
struct Graph
{
int arc[MAX][MAX];
int v_num;
int e_num;
};
int main()
{
freopen("in.txt","r",stdin);
Graph g;
int i,j,k,n,m,w;
while(cin >> g.v_num && g.v_num != 0)
{
for(i = 1;i <= g.v_num;++i)
{
for(j = 1;j <= g.v_num;++j)
g.arc[i][j] = INF;
g.arc[i][i] = 0;
}
for(i = 1;i <= g.v_num;++i)
{
cin >> n;
for(j = 0;j < n;++j)
{
cin >> m >> w;
g.arc[i][m] = w;
}
}
for(k = 1;k <= g.v_num;++k)
{
for(i = 1;i <= g.v_num;++i)
{
for(j = 1;j <= g.v_num;++j)
if(g.arc[i][k] != INF && g.arc[k][j] != INF &&
g.arc[i][k] + g.arc[k][j] < g.arc[i][j])
g.arc[i][j] = g.arc[i][k] + g.arc[k][j];
}
}
m = INF;
for(i = 1;i <= g.v_num;++i)
{
n = 0;
for(j = 1;j <= g.v_num;++j)
{
if(n < g.arc[i][j])
n = g.arc[i][j];
}
if(m > n)
{
m = n;
w = i;
}
}
cout << w << ' ' << m << endl;
}
return 0;
}