-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1125.cc
86 lines (77 loc) · 2.39 KB
/
1125.cc
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
82
83
84
85
86
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
struct Tag {
int sum, cost, person;
Tag() {}
Tag(int s, int c, int p) : sum(s), cost(c), person(p) {}
};
bool operator <(const Tag &t1, const Tag &t2) {
return t2.sum < t1.sum;
}
vector<vector<pair<int, int> > > connection;
vector<bool> used(0, false);
int main() {
while(true) {
int n;
cin >> n;
if(n == 0) break;
connection.clear();
connection.resize(n+1);
for(int i = 1; i <= n; ++i) {
int m;
cin >> m;
for(int j = 1; j <= m; ++j) {
int to, cost;
cin >> to >> cost;
connection[i].push_back(make_pair(cost, to));
}
}
int beststart = -1;
int besttime = INT_MAX;
for(int i = 1; i <= n; ++i) {
priority_queue<Tag> q;
int maxcost = 0;
for(int j = 0; j < connection[i].size(); ++j) {
q.push(Tag(connection[i][j].first, connection[i][j].first, connection[i][j].second));
}
used.clear();
used.resize(n+1);
for(int j = 0; j <= n; ++j) used[j] = false;
used[i] = true;
while(!q.empty()) {
Tag t = q.top();
q.pop();
if(used[t.person]) continue;
used[t.person] = true;
if(t.sum > maxcost) maxcost = t.sum;
//cout << t.cost << " " << t.person << endl;
//if(i == 3) cout << t.person << " " << t.sum << endl;
for(int j = 0; j < connection[t.person].size(); ++j) {
if(used[connection[t.person][j].second]) continue;
q.push(Tag(t.sum+connection[t.person][j].first, connection[t.person][j].first, connection[t.person][j].second));
}
}
bool ok = true;
for(int j = 1; j <= n; ++j) {
if(!used[j]) {
ok = false;
break;
}
}
if(ok && besttime > maxcost) {
besttime = maxcost;
beststart = i;
}
}
if(beststart == -1) {
cout << "disjoint" << endl;
}
else {
cout << beststart << " " << besttime << endl;
}
}
return 0;
}