用floyd算法求出任意两点之间的距离,然后从一点出发找到其他点的最大的距离,找从所有点出发到其他点里的最大距离这一集合的最小值,就是所求.
#include<stdio.h> #include<string.h> #define MAXN 110 int n, m, d[MAXN][MAXN]; int main() { while(scanf("%d",&n)) { if(n == 0) break; memset(d,0x3f,sizeof(d)); for(int i = 1; i <= n; i ++) { scanf("%d",&m); for(int j = 0; j < m; j ++) { int a, b; scanf("%d%d",&a,&b); d[i][a] = b; } } for(int k = 1; k <= n; k ++) for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) { if(d[i][j] > d[i][k] + d[k][j]) d[i][j] = d[i][k] + d[k][j]; } int max; int min = 0x7fffffff; int flag = 0; for(int i = 1; i <= n; i ++) { max = 0; for(int j = 1; j <= n; j ++) { if(i != j && max < d[i][j]) max = d[i][j]; } if(min > max) {min = max;flag = i;} } if(min > 10000000) printf("disjoint\n"); else printf("%d %d\n",flag,min); } return 0; }