uva11383

Golden Tiger Claw

Time Limit: 8 Second

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  where  is the number assigned to the cell located at i’th row and j’th column,  is the number assigned to i’th row and  is the number assigned to j’th column. That is simple isn’t it? Well… the main part is that you have to minimize .

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.

 

SAMPLE INPUT

OUTPUT FOR SAMPLE INPUT

2

1 1

1 1

 

1 1

0 0

2

 



题意:

给一个n*n的矩阵,每个格子中有正整数g[i][j],试为每行和每列分别确定一个数字r[i]和c[i],使得任意格子g[i][j]<=r[i]+c[j]恒成立,并且所有行列和最小。先输行,再输出列,再输出最小和。


这是对KM算法的理解考察嘛,本题利用KM算法l(x)+l(y)>=w(x,y)的性质直接可以知道得出的顶标之和即为最小的,按照要求输出lx ,ly ,KM()即可;KM算法详解

ac代码:

/**复杂度 nx*nx*ny
   求最大权匹配
   最小权匹配,权值取反
   点从0开始
*/
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=550;
const int inf=0x3f3f3f3f;
int nx,ny;
int g[N][N];///二分图 描述
int linker[N],lx[N],ly[N];///y中 个点匹配状态  ,x,y中的点标号
int slack[N];
int visx[N],visy[N];
bool  dfs(int x)
{
    visx[x]=1;
    for(int y=0;y<ny;y++)
    {
        if(visy[y])continue;
        int tmp=lx[x]+ly[y]-g[x][y];
        if(tmp==0)
        {
            visy[y]=1;
            if(linker[y]==-1||dfs(linker[y]))
            {
                linker[y]=x;
                return true;
            }
        }
        else if(slack[y]>tmp)
            slack[y]=tmp;
    }
    return false;
}
int KM()
{
    int i,j;
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for( i=0;i<nx;i++)
    {
        for( j=0,lx[i]=-inf;j<ny;j++)
            if(g[i][j]>lx[i])
            lx[i]=g[i][j];
    }

    for( int x=0;x<nx;x++)
    {
        for( i=0;i<ny;i++)
            slack[i]=inf;
        while(true)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            if(dfs(x))
                break;
            int d=inf;
            for( i=0;i<ny;i++)
             if(!visy[i]&&d>slack[i])
                   d=slack[i];
            for( i=0;i<nx;i++)
                if(visx[i])
                lx[i]-=d;
            for( i=0;i<ny;i++)
            {
                if(visy[i])
                    ly[i]+=d;
                else
                    slack[i]-=d;
            }
        }
    }
    int res=0;
    for( i=0;i<ny;i++)
        if(linker[i]!=-1)
        res+=g[linker[i]][i];
    return res;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
          nx=ny=n;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            scanf("%d",&g[i][j]);
        int ans=KM();
        
        printf("%d",lx[0]);
        for(int i=1;i<nx;i++)
            printf(" %d",lx[i]);
        puts("");
        printf("%d",ly[0]);
        for(int i=1;i<ny;i++)
            printf(" %d",ly[i]);
       printf("\n%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值