-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10278.cpp
114 lines (103 loc) · 2.86 KB
/
P10278.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
typedef PI WeightAndVertex;
#define INF numeric_limits<int>::max()
/*
Dijkstra for shortest path.
N vertices
M edges
*/
int dijkstraMaxDist(int N, vector<WeightAndVertex> *adjacencyLists, vector<int> sources, int newSource) {
//cerr << "Dijkstra(|V|=" << N << ",source=" << source << ",destination=" << destination << ")" << endl;
bool *visited = new bool[N];
int *minPath = new int[N];
FORI(N) {
visited[i] = false;
minPath[i] = INF;
}
set<WeightAndVertex> Q; // To visit
FORIT(vector<int>, sources) {
minPath[*it] = 0;
Q.insert(WeightAndVertex(0, *it));
}
minPath[newSource] = 0;
Q.insert(WeightAndVertex(0, newSource));
while(!Q.empty()) {
const WeightAndVertex p = *Q.begin();
Q.erase(Q.begin());
const int from = p.second;
if(visited[from])
continue;
const int weight = p.first;
// perform relaxation:
//cerr << " " << from << " w=" << weight << " => relax from " << minPath[from] << " to " << weight << endl;
visited[from] = true;
FORIT(vector<WeightAndVertex>, adjacencyLists[from]) {
int neighbour = it->second;
if(visited[neighbour])
continue;
int neighbourWeight = weight + it->first;
if(minPath[neighbour] <= neighbourWeight)
continue;
//cerr << " " << from << "-->" << neighbour << " w=" << it->first << " => from " << minPath[neighbour] << " to " << neighbourWeight << endl;
minPath[neighbour] = neighbourWeight;
Q.insert(WeightAndVertex(neighbourWeight, neighbour));
}
}
int max = 0;
FORI(N) {
if(minPath[i] > max)
max = minPath[i];
}
delete[] visited;
delete[] minPath;
return max;
}
/*
Widest path problem / the bottleneck shortest path problem
*/
int main() {
string line;
int f, N, s, t, w;
vector<WeightAndVertex> adjacencyLists[501];
getline(cin, line); // cases
int cases = atoi(line.c_str());
getline(cin, line); // empty line
for(int cas = 0; cas < cases; ++cas) {
if(cas > 0)
cout << endl;
getline(cin, line); // f and N.
stringstream ss1; ss1 << line; ss1 >> f >> N;
// Read fire stations:
vector<int> fireStations;
FORI(f) {
getline(cin, line); // f and N.
s = atoi(line.c_str()); --s;
fireStations.push_back(s);
}
// Read intersections:
FORI(N) {
adjacencyLists[i].clear();
}
while(true) {
getline(cin, line);
if(cin.eof() || line.empty())
break; // s t and w
stringstream ss2; ss2 << line;
if(!(ss2 >> s >> t >> w))
die();
--s; --t;
adjacencyLists[s].push_back(WeightAndVertex(w, t));
adjacencyLists[t].push_back(WeightAndVertex(w, s));
}
// Find result:
int best = INF;
int whereBest = 0;
FORI(N) {
int d = dijkstraMaxDist(N, adjacencyLists, fireStations, i);
if(d < best) {
best = d;
whereBest = i;
}
}
cout << whereBest+1 << endl;
}
}