-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_assignment.cpp
78 lines (60 loc) · 1.73 KB
/
task_assignment.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
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e6 + 5;
/*
* n <= m
* O(n^2 * m)
* C is 0-based
*/
template<typename T>
T min_cost_assignment(int n, int m, vector<vector<T>> &C, vector<int> &assignment) {
vector<T> u(n + 1), v(m + 1);
vector<int> p(m + 1), way(m + 1);
for (int i = 1; i <= n; i++) {
p[0] = i;
int j0 = 0;
vector<T> minv(m + 1, INF);
vector<bool> used(m + 1, false);
do {
used[j0] = true;
T delta = INF;
int i0 = p[j0], j1;
for (int j = 1; j <= m; j++)
if (!used[j]) {
T curr = C[i0 - 1][j - 1] - u[i0] - v[j];
if (curr < minv[j]) minv[j] = curr, way[j] = j0;
if (minv[j] < delta) delta = minv[j], j1 = j;
}
for (int j = 0; j <= m; j++) {
if (!used[j]) minv[j] -= delta;
else u[p[j]] += delta, v[j] -= delta;
}
j0 = j1;
} while (p[j0]);
do {
int j1 = way[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0);
}
assignment.resize(n);
for (int i = 1; i <= m; i++) {
if (!p[i]) continue;
assignment[p[i] - 1] = i - 1;
}
return -v[0];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
vector<vector<int>> c(n, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> c[i][j];
vector<int> assignment;
int cost = min_cost_assignment(n, n, c, assignment);
cout << cost << "\n";
for (int i = 0; i < n; i++)
cout << (i + 1) << " " << (assignment[i] + 1) << "\n";
}