您的位置:首页 > 编程语言 > Go语言

UVA 11383 Golden Tiger Claw

2015-07-27 09:54 489 查看

Golden Tiger Claw

Time Limit: 8000ms
Memory Limit: 131072KB
This problem will be judged on UVA. Original ID: 11383
64-bit integer IO format: %lld Java class name: Main

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) \leq row(i) + col(j) $where is the number assigned to the cell located at ith row and jth column, row(i) is the number assigned to ith row and col(j) is the number assigned to jth column. That is simple isnt it? Well the main part is that you have to minimize $\sum_{1\leq i \leq n}(row(i) + col(i))$.

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

2

1 1

1 1

OUTPUT FOR SAMPLE INPUT

1 1

0 0

2

解题:来自大白书上的题目,KM算法的一个副产品,$L(x) + L(y) \geq w(x,y)$ KM算法结束后,所有顶标之和是最小的.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
int W[maxn][maxn],Lx[maxn],Ly[maxn],slack[maxn];
int n,Link[maxn];
bool S[maxn],T[maxn];
bool match(int u){
S[u] = true;
for(int v = 0; v < n; ++v){
if(T[v]) continue;
int d = Lx[u] + Ly[v] - W[u][v];
if(!d){
T[v] = true;
if(Link[v] == -1 || match(Link[v])){
Link[v] = u;
return true;
}
}else if(slack[v] > d) slack[v] = d;
}
return false;
}
void update(){
int d = INF;
for(int v = 0; v < n; ++v)
if(!T[v] && slack[v] < d)
d = slack[v];
for(int u = 0; u < n; ++u){
if(S[u]) Lx[u] -= d;
if(T[u]) Ly[u] += d;
else slack[u] -= d;
}
}
int KM(){
for(int u = 0; u < n; ++u){
Lx[u] = -INF;
Ly[u] = 0;
Link[u] = -1;
for(int v = 0; v < n; ++v)
Lx[u] = max(Lx[u],W[u][v]);
}
for(int u = 0; u < n; ++u){
for(int v = 0; v < n; ++v)
slack[v] = INF;
while(true){
memset(S,false,sizeof S);
memset(T,false,sizeof T);
if(match(u)) break;
update();
}
}
int ret = 0;
for(int v = 0; v < n; ++v)
if(Link[v] > -1) ret += W[Link[v]][v];
return ret;
}
int main(){
while(~scanf("%d",&n)){
memset(W,0,sizeof W);
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
scanf("%d",W[i] + j);
int ret = KM();
for(int i = 0; i < n; ++i)
printf("%d%c",Lx[i],i + 1 == n?'\n':' ');
for(int i = 0; i < n; ++i)
printf("%d%c",Ly[i],i + 1 == n?'\n':' ');
printf("%d\n",ret);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: