【UVA 11383】 Golden Tiger Claw (KM算法副产物)
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) ≤ row(i) + col(j) where w(i, j) is the number assigned to the cell located
at i-th row and j-th column, row(i) is the number assigned to i-th row and col(j) is the number
∑
assigned to j-th column. That is simple isnt it? Well . . . the main part is that you have to minimize
1≤i≤n
(row(i) + col(j)).
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.
Note: Be careful about the output format. You may get Wrong Answer if you don’t output properly.
Sample Input
2
1 1
1 1
Sample Output
1 1
0 0
2
【题意】
给出一个n*n的矩阵(n<=500)给每一行x[i],每一列标号y[i],使得对任意a[i][j],x[i]+y[j]>=a[i][j]求行标与列标和最小
【分析】
事实上和最佳匹配没什么关系,但是我们进行KM算法的时候,有w(i,j)<=row(i)+col(j),并且算出来的顶标之和是最小的,so。。。
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 #include<cmath> 8 using namespace std; 9 #define Maxn 510 10 #define Maxm 250010 11 #define INF 0xfffffff 12 13 struct node 14 { 15 int x,y,c,next; 16 }t[Maxm];int len; 17 int first[Maxn]; 18 19 int mymin(int x,int y) {return x<y?x:y;} 20 int mymax(int x,int y) {return x>y?x:y;} 21 22 void ins(int x,int y,int c) 23 { 24 t[++len].x=x;t[len].y=y;t[len].c=c; 25 t[len].next=first[x];first[x]=len; 26 } 27 28 int a[Maxn][Maxn]; 29 int n; 30 31 int lx[Maxn],ly[Maxn],match[Maxn],slack[Maxn]; 32 bool visx[Maxn],visy[Maxn]; 33 34 bool ffind(int x) 35 { 36 visx[x]=1; 37 for(int i=first[x];i;i=t[i].next) if(!visy[t[i].y]) 38 { 39 int y=t[i].y; 40 if(t[i].c==lx[x]+ly[y]) 41 { 42 visy[y]=1; 43 if(!match[y]||ffind(match[y])) 44 { 45 match[y]=x; 46 return 1; 47 } 48 } 49 else slack[y]=mymin(slack[y],lx[x]+ly[y]-t[i].c); 50 } 51 return 0; 52 } 53 54 void solve() 55 { 56 memset(match,0,sizeof(match)); 57 memset(lx,0,sizeof(lx)); 58 memset(ly,0,sizeof(ly)); 59 for(int i=1;i<=n;i++) 60 for(int j=first[i];j;j=t[j].next) lx[i]=mymax(lx[i],t[j].c); 61 62 for(int i=1;i<=n;i++) 63 { 64 for(int j=1;j<=n;j++) 65 slack[j]=INF; 66 while(1) 67 { 68 memset(visx,0,sizeof(visx)); 69 memset(visy,0,sizeof(visy)); 70 if(ffind(i)) break; 71 int delta=INF; 72 for(int j=1;j<=n;j++) 73 { 74 if(!visy[j]) 75 { 76 delta=mymin(delta,slack[j]); 77 } 78 } 79 if(delta==INF) return; 80 for(int j=1;j<=n;j++) 81 { 82 if(visx[j]) lx[j]-=delta; 83 if(visy[j]) ly[j]+=delta; 84 else slack[j]-=delta; 85 } 86 } 87 } 88 } 89 90 int main() 91 { 92 while(scanf("%d",&n)!=EOF) 93 { 94 len=0; 95 memset(first,0,sizeof(first)); 96 for(int i=1;i<=n;i++) 97 for(int j=1;j<=n;j++) 98 { 99 int x; 100 scanf("%d",&x); 101 ins(i,j,x); 102 } 103 solve(); 104 for(int i=1;i<=n;i++) printf("%d ",lx[i]);printf("\n"); 105 for(int i=1;i<=n;i++) printf("%d ",ly[i]);printf("\n"); 106 int ans=0; 107 for(int i=1;i<=n;i++) ans+=lx[i]+ly[i]; 108 printf("%d\n",ans); 109 } 110 return 0; 111 }
2016-10-27 15:13:52