-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstoer_wagner.cpp
46 lines (39 loc) · 1.08 KB
/
stoer_wagner.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
/*
Tested: ZOJ 2753
Complexity: O(n^3)
*/
template <typename T>
pair<T, vector<int>> stoer_wagner(vector<vector<T>> &weights) {
int n = weights.size();
vector<int> used(n), cut, best_cut;
T best_weight = -1;
for (int phase = n - 1; phase >= 0; --phase) {
vector<T> w = weights[0];
vector<int> added = used;
int prev, last = 0;
for (int i = 0; i < phase; ++i) {
prev = last;
last = -1;
for (int j = 1; j < n; ++j)
if (!added[j] && (last == -1 || w[j] > w[last]))
last = j;
if (i == phase - 1) {
for (int j = 0; j < n; ++j)
weights[prev][j] += weights[last][j];
for (int j = 0; j < n; ++j)
weights[j][prev] = weights[prev][j];
used[last] = true;
cut.push_back(last);
if (best_weight == -1 || w[last] < best_weight) {
best_cut = cut;
best_weight = w[last];
}
} else {
for (int j = 0; j < n; j++)
w[j] += weights[last][j];
added[last] = true;
}
}
}
return make_pair(best_weight, best_cut);
}