UVA11383 Golden Tiger Claw——KM求二分图匹配
作者:互联网
题目
Omi, Raymondo, Clay and Kimiko are on new adventure- n search of new Shen Gong Wu. But Evil Boy Genius Jack Spicer is also there. Omi and Jack found the Shen Gong Wu at the same time so they rushed for it but alas they touched it at the same time. Then what? It is time for “Xiaolin Showdown”.
Jack challenged Omi to play a game. The game is simple! There will be an \(N ∗ N\) board where each cell in the board contains some number. They have to assign numbers to each row and column separately so that \(w(i, j) ≤ row(i) + col(j)\) where \(w(i, j)\) is the number assigned to the cell located at \(i\)-th row and \(j\)-th column, \(row(i)\) is the number assigned to \(i\)-th row and \(col(j)\) is the number assigned to \(j\)-th column. That is simple isnt it? Well . . . the main part is that you have to minimize \(\sum_{1≤i≤n} (row(i) + col(j))\).
Jack has taken his favorite “Monkey Stuff” and Omi has taken “Golden Tiger Claw”. With the help of this “Golden Tiger Claw”, he can go anywhere in the world. He has come to you and seeking your help. Jack is using his computer to solve this problem. So do it quick! Find the most optimal solution for Omi so that you can also be part of history in saving the world from the darkness of evil.
Input
Input contains 15 test cases. Each case starts with \(N\). Then there are \(N\) lines containing \(N\) numbers each. All the numbers in input is positive integer within the limit \(100\) except \(N\) which can be at most\(500.\)
Output
For each case in the first line there will be \(N\) numbers, the row assignments. In the next line there
will \(N\) column assignment. And at the last line the minimum sum should be given. If there are several
possible solutions give any.
Note:
Be careful about the output format.
You may get Wrong Answer if you don’t output properly.
Sample Input
2
1 1
1 1
Sample Output
1 1
0 0
2
题解
题意翻译
给定一个\(N*N\)的矩阵,每个格子都有一个权值\(w_{i,j}\),
给每行确定一个整数\(h_i\),每列确定一个整数\(l_j\),
使得对于任意格子,\(w_{i,j}\leq h_i+l_j\),
并使得\(\sum_{i=1}^nh_i\)和\(\sum_{j=1}^nl_j\)的和尽量小。
有多组数据
解题思路
毕竟刚学了KM算法,题目要求满足\(w_{i,j}\leq h_i+l_j\),就联想到KM算法
就是一道KM板子题,没什么好说的。
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 505, M = 0x7fffffff;
int n, a[N][N], m[N], lx[N], ly[N], s[N], vx[N], vy[N];
void init() {
memset(m, -1, sizeof(m));
memset(lx, 0xcf, sizeof(lx));
memset(ly, 0, sizeof(ly));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
lx[i] = max(lx[i], a[i][j]);
}
int dfs(int x) {
vx[x] = 1;
for(int i = 1; i <= n; i++)
if (!vy[i]) {
int t = lx[x] + ly[i] - a[x][i];
if (!t) {
vy[i] = 1;
if (m[i] == -1 || dfs(m[i])) {
m[i] = x;
return 1;
}
}
else s[i] = min(s[i], t);
}
return 0;
}
void km() {
init();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++)
s[j] = M;
while (1) {
memset(vx, 0, sizeof(vx));
memset(vy, 0, sizeof(vy));
if (dfs(i)) break;
int z = M;
for(int j = 1; j <= n; j++)
if (!vy[j]) z = min(z, s[j]);
for(int j = 1; j <= n; j++) {
if (vx[j]) lx[j] -= z;
if (vy[j]) ly[j] += z;
}
}
}
}
void print() {
int ans = 0;
for(int i = 1; i <= n; i++)
printf("%d ", lx[i]), ans += lx[i];
puts("");
for(int i = 1; i <= n; i++)
printf("%d ", ly[i]), ans += ly[i];
printf("\n%d\n", ans);
}
int main() {
while (scanf("%d", &n) == 1) {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
km();
print();
}
return 0;
}
标签:Golden,Omi,there,UVA11383,KM,Jack,th,each,row 来源: https://www.cnblogs.com/Z8875/p/12868414.html