zoukankan      html  css  js  c++  java
  • uva 11383 Golden Tiger Claw km算法

    Problem G

    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

    Problemsetter: Md. Mahbubul Hasan

    (Be careful about the output format. You may get Wrong Answer if you don’t output properly)

    看到题目给的不等式我也想到了km算法中的不等式,然后要求lx,ly总值最小,于是想到最小匹配,但这样求出来的最终结果会不满足不等式。。。还是最大匹配吧。。。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    
    #define MAXN 505
    #define INF 111
    
    int lx[MAXN], ly[MAXN], ytox[MAXN];
    int n, slack[MAXN];
    bool s[MAXN], t[MAXN];
    int w[MAXN][MAXN];
    
    bool dfs(int x){
        s[x]=true;
        for(int y=1; y<=n; y++){
            if(t[y]) continue;
            int d=lx[x]+ly[y]-w[x][y];
            if(!d){
                t[y]=true;
                if(!ytox[y] || dfs(ytox[y])){
                    ytox[y]=x;
                    return true;
                }
            }
            else slack[y]=MIN(slack[y], d);
        }
        return false;
    }
    
    void km(){
        int i, j;
    
        for(i=1; i<=n; i++)
            for(j=1, lx[i]=INF; j<=n; j++){
                scanf(" %d", &w[i][j]);         //w[i][j]=-w[i][j];
                lx[i]=MAX(lx[i], w[i][j]);
            }
        memset(ly, 0, sizeof(ly));
        memset(ytox, 0, sizeof(ytox));
    
        for(i=1; i<=n; i++){
            for(j=1; j<=n; j++) slack[j]=INF;
    
            while(1){
                memset(s, 0, sizeof(s));
                memset(t, 0, sizeof(t));
                if(dfs(i)) break;
    
                int d=INF;
                for(j=1; j<=n; j++) if(!t[j])
                    d=MIN(d, slack[j]);
                for(j=1; j<=n; j++) if(s[j])
                    lx[j]-=d;
                for(j=1; j<=n; j++) if(t[j])
                    ly[j]+=d;
            }
        }
        int ans=0;
        for(i=1; i<=n; i++) printf("%d%c", lx[i], (i==n ? '
    ' : ' ')), ans+=lx[i];
        for(i=1; i<=n; i++) printf("%d%c", ly[i], (i==n ? '
    ' : ' ')), ans+=ly[i];
        printf("%d
    ", ans);
    }
    
    int main(){
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        while(scanf(" %d", &n)==1) km();
        return 0;
    }
    
  • 相关阅读:
    通过加载Xib文件来创建UITableViewCell造成复用数据混乱问题方案
    iOS开发过程中常见错误问题及解决方案
    iOS开发常用第三方库
    KVC和KVO的理解(底层实现原理)
    iOS面试必备-iOS基础知识
    iOS应用适配IPV6
    Runtime运行时的那点事儿
    iOS应用性能调优的25个建议和技巧
    iOS清除缓存功能开发
    微信浏览器跳转页面后再返回,如何恢复到跳转前的位置的问题。
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3318327.html
Copyright © 2011-2022 走看看