-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP11463.cpp
83 lines (70 loc) · 2.29 KB
/
P11463.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
typedef pair<int,int> WeightAndVertex;
#define INF numeric_limits<int>::max()
/*
Dijkstra for shortest path.
N vertices
*/
void dijkstra(int N, vector<WeightAndVertex> *adjacencyLists, int source, int *minPath) {
//cerr << "Dijkstra(|V|=" << N << ",source=" << source << ",destination=" << destination << ")" << endl;
bool *visited = new bool[N];
//int *minPath = new int[N];
for(int i = 0; i < N; ++i) {
visited[i] = false;
minPath[i] = INF;
}
minPath[source] = 0;
set<WeightAndVertex> Q; // To visit
Q.insert(WeightAndVertex(0, source));
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;
for(vector<WeightAndVertex>::const_iterator it = adjacencyLists[from].begin(); it != adjacencyLists[from].end(); ++it) {
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));
}
}
delete[] visited;
}
/*
Widest path problem / the bottleneck shortest path problem
*/
int main() {
vector<WeightAndVertex> adjacencyLists[101];
int N, M, s, t, minS[101], minT[101]; // N nodes, M edges
FORCAS {
cin >> N >> M;
FORI(N) {
adjacencyLists[i].clear();
}
FORI(M) {
cin >> s >> t;
adjacencyLists[s].push_back(WeightAndVertex(1,t));
adjacencyLists[t].push_back(WeightAndVertex(1,s));
}
cin >> s >> t;
// Find maximum flow path between S and D:
dijkstra(N, adjacencyLists, s, minS);
dijkstra(N, adjacencyLists, t, minT);
int max = 0;
FORI(N) {
int x = minS[i]+minT[i];
//cerr << "STEPPING " << i << ": " << minS[i]<<"+"<<minT[i] << "=" << x << endl;
max = MAX(x, max);
}
cout << "Case " << cas+1 << ": " << max << endl;
}
}