uva11383
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
|
题意:
给一个n*n的矩阵,每个格子中有正整数w[i][j],试为每行和每列分别确定一个数字row[i]和col[i],使得任意格子w[i][j]<=row[i]+col[j]恒成立。先输row,再输出col,再输出全部总和(总和应尽量小)。
思路:
本题可以用KM算法解决。
KM算法中的顶标就是保持了l1[i]+l2[j]>=tu[i][j]再求最大权和匹配的,但这个最大权和并没有关系。我们可以将row[i]看成l1[i],col[i]看成l2[i]。
一般来说,l1[i]或l2[i]仅需要取该行/列中最大的那个数即可保证满足要求,但是这样太大了,可以通过调整来使得总和更小。而KM算法的过程就是一个调整的过程,每一对匹配的那条边的权值就会满足等号 tu[i][j]=row[i]+col[j],至少需要一个来满足等号,这样才能保证row[i]+col[j]是达到最小的,即从j列看,col[j]满足条件且最小,从i行看,row[i]满足条件且最小。这刚好与KM算法求最大权和一样。
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<string.h> #include<algorithm> #include<math.h> #include<queue> using namespace std; typedef long long ll; const int inf=1e9,maxn=510; int KM(int m,int n,int tu[][maxn],int *match1,int *match2) { int s[maxn],t[maxn],l1[maxn],l2[maxn],p,q,ret=0,i,j,k; ///l1为左边的匹配分量,l2是右边的匹配分量 for(i=0; i<m; i++) { for(l1[i]=-inf,j=0; j<n; j++) l1[i]=tu[i][j]>l1[i]?tu[i][j]:l1[i]; if(l1[i]==-inf) return -1; } for(i=0; i<n; l2[i++]=0); memset(match1,-1,sizeof(int)*n); memset(match2,-1,sizeof(int)*n); for(i=0; i<m; i++) { memset(t,-1,sizeof(int)*n); for(s[p=q=0]=i; p<=q&&match1[i]<0; p++) { for(k=s[p],j=0; j<n&&match1[i]<0; j++) if(l1[k]+l2[j]==tu[k][j]&&t[j]<0) { s[++q]=match2[j],t[j]=k; if(s[q]<0) for(p=j; p>=0; j=p) match2[j]=k=t[j],p=match1[k],match1[k]=j; } } if(match1[i]<0) { for(i--,p=inf,k=0; k<=q; k++) for(j=0; j<n; j++) if(t[j]<0&&l1[s[k]]+l2[j]-tu[s[k]][j]<p) p=l1[s[k]]+l2[j]-tu[s[k]][j]; for(j=0; j<n; l2[j]+=t[j]<0?0:p,j++); for(k=0; k<=q; l1[s[k++]]-=p); } } for(i=0; i<m; i++) ret+=tu[i][match1[i]]; printf("%d",l1[0]); for(int i=1; i<n; i++) printf(" %d",l1[i]); puts(""); printf("%d",l2[0]); for(int i=1; i<n; i++) printf(" %d",l2[i]); puts(""); return ret; } int n; int tu[maxn][maxn]; int match1[maxn],match2[maxn]; int main() { while(~scanf("%d",&n)) { memset(tu,0,sizeof(tu)); for(int i=0; i<n; i++) for(int j=0; j<n; j++) scanf("%d",&tu[i][j]); printf("%d\n",KM(n,n,tu,match1,match2)); } return 0; }