-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathHungarian Weighted Matching.cpp
68 lines (64 loc) · 1.78 KB
/
Hungarian Weighted Matching.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
// hungarian weighted matching algo
// finds the max cost of max matching, to find mincost, add edges as negatives
// Nodes are indexed from 1 on both sides
template<typename T>
struct KuhnMunkras { // n for left, m for right
int n, m, match[maxM];
T g[maxN][maxM], lx[maxN], ly[maxM], slack[maxM];
bool vx[maxN], vy[maxM];
void init(int n_, int m_) {
ms(g,0); n = n_, m = m_;
}
void add(int u, int v, T w) {
g[u][v] = w;
}
bool find(int x) {
vx[x] = true;
for (int y = 1; y <= m; ++y) {
if (!vy[y]) {
T delta = lx[x] + ly[y] - g[x][y];
if (delta==0) {
vy[y] = true;
if (match[y] == 0 || find(match[y])) {
match[y] = x;
return true;
}
} else slack[y] = min(slack[y], delta);
}
}
return false;
}
T matching() { // maximum weight matching
fill(lx + 1, lx + 1 + n, numeric_limits<T>::lowest());
ms(ly,0);
ms(match,0);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) lx[i] = max(lx[i], g[i][j]);
}
for (int k = 1; k <= n; ++k) {
fill(slack + 1, slack + 1 + m, numeric_limits<T>::max());
while (true) {
ms(vx,0);
ms(vy,0);
if (find(k)) break;
else {
T delta = numeric_limits<T>::max();
for (int i = 1; i <= m; ++i) {
if (!vy[i]) delta = min(delta, slack[i]);
}
for (int i = 1; i <= n; ++i) {
if (vx[i]) lx[i] -= delta;
}
for (int i = 1; i <= m; ++i) {
if (vy[i]) ly[i] += delta;
if (!vy[i]) slack[i] -= delta;
}
}
}
}
T result = 0;
for (int i = 1; i <= n; ++i) result += lx[i];
for (int i = 1; i <= m; ++i) result += ly[i];
return result;
}
};