forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1125(AC).cpp
81 lines (72 loc) · 1.26 KB
/
POJ1125(AC).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cstdio>
using namespace std;
const int MAXN = 102;
int a[MAXN][MAXN];
int n;
const int INF = 1000000000;
int main()
{
int i, j, k;
int nk;
int x, y;
bool connected;
int min_max_len, max_len, max_i;
while (scanf("%d", &n) == 1 && n) {
connected = false;
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
a[i][j] = INF;
}
}
for (i = 1; i <= n; ++i) {
scanf("%d", &nk);
for (j = 1; j <= nk; ++j) {
scanf("%d%d", &x, &y);
a[i][x] = y;
}
}
for (k = 1; k <= n; ++k) {
for (i = 1; i <= n; ++i) {
if (a[i][k] >= INF) {
continue;
}
for (j = 1; j <= n; ++j) {
if (a[k][j] >= INF) {
continue;
}
if (a[i][j] > a[i][k] + a[k][j]) {
a[i][j] = a[i][k] + a[k][j];
}
}
}
}
max_i = -1;
min_max_len = INF;
for (i = 1; i <= n; ++i) {
max_len = -1;
for (j = 1; j <= n; ++j) {
if (i == j) {
continue;
}
if (a[i][j] == INF) {
break;
} else if(a[i][j] > max_len) {
max_len = a[i][j];
}
}
if (j > n) {
connected = true;
if (max_len < min_max_len) {
min_max_len = max_len;
max_i = i;
}
}
}
if (connected) {
printf("%d %d\n", max_i, min_max_len);
} else {
printf("disjoint\n");
}
}
return 0;
}