题目描述
解题思路
找出一个人,使谣言从其传出到所有人用时最短.
(1):用Floyd先算出某人到每个人的最短时间,再对时间取最大 (保证每个人都能收到谣言…) = =|
(2):对每个人执行(1),取时间最小,即为所求.
参考代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define inf 0x3f3f3f3f
#define MAX_V 110
using namespace std;
int f[MAX_V][MAX_V],a[MAX_V];
int main()
{
int n,num,u,cost;
while(~scanf("%d",&n),n){
memset(f,inf,sizeof(f));
memset(a,0,sizeof(a));
int num;
for(int i = 1;i <= n;i++){
scanf("%d",&num);
while(num--){
scanf("%d %d",&u,&cost);
f[i][u]=cost;
}
}
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(i!=k && i!=j && j!=k)
f[i][j] = min(f[i][j],f[i][k]+f[k][j]);
int ans = inf,t;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++)
if(i != j && (f[i][j]>a[i]))
a[i] = f[i][j];
if(a[i] < ans){
ans = a[i];
t = i;
}
}
if(ans == inf)printf("disjoint\n");
else printf("%d %d\n",t,ans);
}
return 0;
}