-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2129.cpp
57 lines (55 loc) · 1.17 KB
/
2129.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
#include <bits/stdc++.h>
using namespace std;
const int N = 205, INF = 1e9;
int n, c[N][N], u[N], v[N], p[N], d[N], trace[N];
bool used[N];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> c[i][j];
}
}
for (int i = 1; i <= n; ++i) {
p[0] = i;
int j0 = 0;
fill(d + 1, d + 1 + n, INF);
fill(used + 1, used + 1 + n, false);
do {
used[j0] = true;
int i0 = p[j0], delta = INF, j1;
for (int j = 1; j <= n; ++j) {
if (!used[j]) {
int cur = c[i0][j] - u[i0] - v[j];
if (cur < d[j]) {
d[j] = cur;
trace[j] = j0;
}
if (d[j] < delta) {
delta = d[j];
j1 = j;
}
}
}
for (int j = 0; j <= n; ++j) {
if (used[j]) {
u[p[j]] += delta;
v[j] -= delta;
} else {
d[j] -= delta;
}
}
j0 = j1;
} while (p[j0]);
do {
int j1 = trace[j0];
p[j0] = p[j1];
j0 = j1;
} while (j0);
}
cout << -v[0] << "\n";
for (int i = 1; i <= n; ++i) {
cout << p[i] << " " << i << "\n";
}
return 0;
}