ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UVA11383 Golden Tiger Claw——KM求二分图匹配

2020-05-11 13:02:01  阅读:350  来源: 互联网

标签:Golden Omi there UVA11383 KM Jack th each row


题目

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有