floyd找出多源最短路后从中选择一个源点。
以该源点出发最大花费最小即满足条件。
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 105;
const int inf = 1000 + 24;
int d[maxn][maxn];
void floyd(int n) {
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
int main(int argc, char const *argv[]) {
int t;
while (scanf("%d", &t) == 1 && t) {
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
d[i][j] = inf;
if (i == j) {
d[i][j] = 0;
}
}
}
for (int i = 0; i < t; i++) {
int n;
scanf("%d", &n);
for (int j = 0; j < n; j++) {
int id, cost;
scanf("%d%d", &id, &cost);
id--;
d[i][id] = cost;
}
}
floyd(t);
int maxcost = 0;
int ans;
int cost = inf;
for (int i = 0; i < t; i++) {
maxcost = *max_element(d[i], d[i] + t);
if (cost > maxcost) {
cost = maxcost;
ans = i;
}
}
if (cost == inf) {
puts("disjoint");
} else {
printf("%d %d\n", ans + 1, cost);
}
}
return 0;
}