Golden Tiger clawtime limit:8000msmemory limit:131072kbthis problem'll be judged onUVA. Original id:11383
64-bit integer IO format: %lld Java class name: Main
Omi, Raymondo, Clay and Kimiko is on new adventure-in search of new Shen Gong Wu. But Evil boy Genius Jack Spicer was also there. Omi and Jack found the Shen Gong Wu at the same time so they rushed for it is alas they touched it at the same time. And what? It is time for Xiaolin Showdown.
Jack challenged Omi to play a game. The game is simple! There'll be a n*n board where each cell in the board contains some number. They has to assign numbers to all row and column separately so $w (i,j) \leq row (i) + col (j) $where is the number as Signed to the cell located at ith row and jth column, row (i) are the number assigned to ith row and Col (j) is the number as Signed to jth column. That's simple isnt it? The well, main part is, you have to minimize $\sum_{1\leq i \leq n} (Row (i) + col (i)) $.
Jack has taken he favorite Monkey Stuff and Omi has taken Golden Tiger Claw. With the "This Golden Tiger Claw", he can go anywhere in the world. He has come to you and seeking your help. Jack is using the computer to solve this problem. So do it quick! Find the most optimal solution for Omi so this you can also is part of the history in saving the world from the darkness of Ev Il.
INPUT
Input contains test cases. Each case is starts with N. Then there is n lines containing n numbers each. All the numbers on input is positive integer within the limit of except N which can be at most 500.
OUTPUT
For each case in the first line there would be N numbers, the row assignments. The next line there would N column assignment. The minimum sum should be given. If there is several possible solutions give any.
SAMPLE INPUT
2
1 1
1 1
OUTPUT for SAMPLE INPUT
1 1
0 0
2
Problem solving: A byproduct from the Great White Book, a by-product of the KM algorithm, $L (x) + L (y) \geq W (x, y) $ km The sum of all the top marks is minimal after the algorithm ends.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =510;4 Const intINF =0x3f3f3f3f;5 intW[MAXN][MAXN],LX[MAXN],LY[MAXN],SLACK[MAXN];6 intN,LINK[MAXN];7 BOOLS[MAXN],T[MAXN];8 BOOLMatchintu) {9S[u] =true;Ten for(intv =0; v < n; ++v) { One if(T[v])Continue; A intD = lx[u] + ly[v]-W[u][v]; - if(!d) { -T[V] =true; the if(Link[v] = =-1||match (Link[v])) { -LINK[V] =u; - return true; - } +}Else if(Slack[v] > D) slack[v] =D; - } + return false; A } at voidUpdate () { - intD =INF; - for(intv =0; v < n; ++v) - if(! T[V] && Slack[v] <d) -D =Slack[v]; - for(intU =0; U < n; ++u) { in if(S[u]) Lx[u]-=D; - if(T[u]) Ly[u] + =D; to ElseSlack[u]-=D; + } - } the intKM () { * for(intU =0; U < n; ++u) { $Lx[u] =-INF;Panax NotoginsengLy[u] =0; -Link[u] =-1; the for(intv =0; v < n; ++v) +Lx[u] =Max (lx[u],w[u][v]); A } the for(intU =0; U < n; ++u) { + for(intv =0; v < n; ++v) -SLACK[V] =INF; $ while(true){ $memset (S),false,sizeofS); -memset (T,false,sizeofT); - if(Match (U)) Break; the update (); - }Wuyi } the intRET =0; - for(intv =0; v < n; ++v) Wu if(Link[v] >-1) ret + =W[link[v]][v]; - returnret; About } $ intMain () { - while(~SCANF ("%d",&N)) { -memset (W,0,sizeofW); - for(inti =0; I < n; ++i) A for(intj =0; J < N; ++j) +scanf"%d", W[i] +j); the intRET =KM (); - for(inti =0; I < n; ++i) $printf"%d%c", Lx[i],i +1= = n?'\ n':' '); the for(inti =0; I < n; ++i) theprintf"%d%c", Ly[i],i +1= = n?'\ n':' '); theprintf"%d\n", ret); the } - return 0; in}View Code
UVA 11383 Golden Tiger Claw