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算法结束后,所有顶标之和是最小的.
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 510; 4 const int INF = 0x3f3f3f3f; 5 int W[maxn][maxn],Lx[maxn],Ly[maxn],slack[maxn]; 6 int n,Link[maxn]; 7 bool S[maxn],T[maxn]; 8 bool match(int u){ 9 S[u] = true; 10 for(int v = 0; v < n; ++v){ 11 if(T[v]) continue; 12 int d = Lx[u] + Ly[v] - W[u][v]; 13 if(!d){ 14 T[v] = true; 15 if(Link[v] == -1 || match(Link[v])){ 16 Link[v] = u; 17 return true; 18 } 19 }else if(slack[v] > d) slack[v] = d; 20 } 21 return false; 22 } 23 void update(){ 24 int d = INF; 25 for(int v = 0; v < n; ++v) 26 if(!T[v] && slack[v] < d) 27 d = slack[v]; 28 for(int u = 0; u < n; ++u){ 29 if(S[u]) Lx[u] -= d; 30 if(T[u]) Ly[u] += d; 31 else slack[u] -= d; 32 } 33 } 34 int KM(){ 35 for(int u = 0; u < n; ++u){ 36 Lx[u] = -INF; 37 Ly[u] = 0; 38 Link[u] = -1; 39 for(int v = 0; v < n; ++v) 40 Lx[u] = max(Lx[u],W[u][v]); 41 } 42 for(int u = 0; u < n; ++u){ 43 for(int v = 0; v < n; ++v) 44 slack[v] = INF; 45 while(true){ 46 memset(S,false,sizeof S); 47 memset(T,false,sizeof T); 48 if(match(u)) break; 49 update(); 50 } 51 } 52 int ret = 0; 53 for(int v = 0; v < n; ++v) 54 if(Link[v] > -1) ret += W[Link[v]][v]; 55 return ret; 56 } 57 int main(){ 58 while(~scanf("%d",&n)){ 59 memset(W,0,sizeof W); 60 for(int i = 0; i < n; ++i) 61 for(int j = 0; j < n; ++j) 62 scanf("%d",W[i] + j); 63 int ret = KM(); 64 for(int i = 0; i < n; ++i) 65 printf("%d%c",Lx[i],i + 1 == n?‘\n‘:‘ ‘); 66 for(int i = 0; i < n; ++i) 67 printf("%d%c",Ly[i],i + 1 == n?‘\n‘:‘ ‘); 68 printf("%d\n",ret); 69 } 70 return 0; 71 }