UVA11383 Golden Tiger Claw km算法

题目描述:

Omi, Raymondo, Clay and Kimiko are on new adventure- in 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
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的矩阵G,每个位置有一个权,求两个一维数组row和col,使row[i] + col[j]>=G[i][j],并且∑row+∑col 最小,输出这个和。
这题看上去挺难看懂,其实就是一个二分图的带权匹配问题。类似与HDU2255http://acm.hdu.edu.cn/showproblem.php?pid=2255的老百姓与房间的匹配。KM算法中的顶标就是最小的row和col。这题看懂了也就是KM的模板。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const int N=550;
const int INF =0x3f3f3f3f;
using namespace std;

int n;
int g[N][N];
int linker[N],lx[N],ly[N];
int slack[N];
bool visx[N],visy[N];

bool dfs(int x)
{
    visx[x]=true;
    for(int y=1; y<=n; y++)
    {
        if (visy[y]) continue;
        int tmp=lx[x]+ly[y]-g[x][y];
        if (tmp==0)
        {
            visy[y]=true;
            if (linker[y]==-1 || dfs(linker[y]))
            {
                linker[y]=x;
                return true;
            }
        }
        else if (slack[y]>tmp) slack[y]=tmp;
    }
    return false;
}

int KM()
{
    memset(linker,-1,sizeof(linker));
    memset(lx,0,sizeof(lx));
    memset(ly,0,sizeof(ly));
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if (g[i][j]>lx[i]) lx[i]=g[i][j];
        }
    }
    for(int x=1; x<=n; x++)
    {
        for(int i=1; i<=n; i++) slack[i]=INF;
        while(1)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if (dfs(x)) break;
            int d=INF;
            for(int i=1; i<=n; i++)
               if (!visy[i] && d>slack[i]) d=slack[i];
            for(int i=1; i<=n; i++)
               if (visx[i]) lx[i]-=d;
            for(int i=1; i<=n; i++)
            {
                if (visy[i]) ly[i]+=d;
                else slack[i]-=d;
            }
        }
    }
    int res=0;
    for(int i=1; i<=n; i++)
       res+=lx[i]+ly[i];
    return res;
}

int main()
{
    while(~scanf("%d",&n))
    {
        memset(g,0,sizeof(g));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%d",&g[i][j]);
            }
        }
        int ans=KM();
        printf("%d",lx[1]);
        for(int i=2; i<=n; i++) printf(" %d",lx[i]);
        printf("\n");
        printf("%d",ly[1]);
        for(int i=2; i<=n; i++) printf(" %d",ly[i]);
        printf("\n");
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(KM算法)