24h購物| | PChome| 登入
2014-03-03 07:48:31| 人氣2,134| 回應0 | 上一篇 | 下一篇

[UVA][KM算法] 11383 - Golden Tiger Claw

推薦 0 收藏 0 轉貼0 訂閱站台

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 算法的核心操作,而這一道問題剛好是副產物。


詳見 : http://www.cnblogs.com/silver-bullet/archive/2013/02/22/2922064.html


#include <stdio.h>
#include <string.h>
int W[505][505], N;
int mx[505], my[505]; // match arr
int lx[505], ly[505]; // label arr
int x[505], y[505]; // used arr
int hungary(int nd) {
    int i;
    x[nd] = 1;
    for(i = 1; i <= N; i++) {
        if(y[i] == 0 && W[nd][i] == lx[nd]+ly[i]) {
            y[i] = 1;
            if(my[i] == 0 || hungary(my[i])) {
                my[i] = nd;
                return 1;
            }
        }
    }
    return 0;
}
int KM() {
    int i, j, k, d;
    memset(mx, 0, sizeof(mx));
    memset(my, 0, sizeof(my));
    memset(lx, 0, sizeof(lx));
    memset(ly, 0, sizeof(ly));
    for(i = 1; i <= N; i++)
        for(j = 1; j <= N; j++)
            lx[i] = lx[i] > W[i][j] ? lx[i] : W[i][j];
    for(i = 1; i <= N; i++) {
        while(1) {
            memset(x, 0, sizeof(x));
            memset(y, 0, sizeof(y));
            if(hungary(i))  break;
            d = 0xfffffff;
            for(j = 1; j <= N; j++) {
                if(x[j]) {
                    for(k = 1; k <= N; k++)
                        if(!y[k])
                        d = d < lx[j]+ly[k]-W[j][k] ?
                            d : lx[j]+ly[k]-W[j][k];
                }
            }
            if(d == 0xfffffff)  break;
            for(j = 1; j <= N; j++) {
                if(x[j])    lx[j] -= d;
                if(y[j])    ly[j] += d;
            }
        }
    }
    int res = 0;
    for(i = 1; i <= N; i++) {
        if(my[i])
            res += W[my[i]][i];
    }
    return res < 0 ? 0 : res;
}
int main() {
    int n;
    while(scanf("%d", &n) == 1 && n) {
        N = n;
        int i, j;
        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                scanf("%d", &W[i][j]);
        KM();
        int ret = 0;
        for(i = 1; i <= n; i++)
            printf("%d%c", lx[i], i == n ? '\n' : ' '), ret += lx[i];
        for(i = 1; i <= n; i++)
            printf("%d%c", ly[i], i == n ? '\n' : ' '), ret += ly[i];
        printf("%d\n", ret);
       
    }
    return 0;
}

台長: Morris
人氣(2,134) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][DP] 11370 - Moogle
此分類上一篇:[UVA][bitmask] 11394 - Digit Blocks

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文