POJ 1125 Stockbroker Grapevine Posted on 2018-09-24 | In ACM | 阅读 Floyd C++ Code 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#define M 101#define INF 1000000using namespace std;int arr[M][M];int main(){ int num, contact; int to, cost; while(scanf("%d", &num) && num != 0){ for(int i = 1; i <= num; i++){ for(int j = 1; j <= num; j++){ if(i == j) arr[i][j] = 0; else arr[i][j] = INF; } } for(int i = 1; i <= num; i++){ scanf("%d", &contact); for(int j = 0; j < contact; j++){ scanf("%d%d", &to, &cost); arr[i][to] = cost; } } for(int k = 1; k <= num; k++){ for(int i = 1; i <= num; i++){ for(int j = 1; j <= num; j++){ arr[i][j] = min(arr[i][j], arr[i][k] + arr[k][j]); } } } bool flag = false; int mi = INF; int pos; for(int i = 1; i <= num; i++){ flag = false; int mx = -1; for(int j = 1; j <= num; j++){ if(arr[i][j] >= INF){ flag = true; break; } mx = max(mx, arr[i][j]); } if(flag == false && mi > mx){ mi = mx; pos = i; } } if(mi == INF) printf("disjoint\n"); else printf("%d %d\n", pos, mi); } return 0;}