码迷,mamicode.com
首页 > 编程语言 > 详细

Golden Tiger Claw (KM算法)

时间:2020-05-11 18:32:58      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:except   note   solution   this   where   careful   start   sam   case   

Golden Tiger Claw (KM算法)

Describe

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

Solution

题意

  给出一个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),并且算出来的顶标之和是最小的.

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=505,Inf=0x3f3f3f3f;
int n,w[maxn][maxn],lx[maxn],ly[maxn],f[maxn];
bool s[maxn],t[maxn];
bool Find(int x){
	s[x]=1;
	for(int i=1;i<=n;++i){
		if(lx[x]+ly[i]==w[x][i]&&!t[i]){
			t[i]=1;
			if(!f[i] ||Find(f[i])){
				f[i]=x;
				return 1;
			}
		}
	}
	return 0;
}
void Upd(){
	int a=Inf;
	for(int i=1;i<=n;++i)
		if(s[i])
			for(int j=1;j<=n;++j)
				if(!t[j])a=min(a,lx[i]+ly[j]-w[i][j]);
	for(int i=1;i<=n;++i){
		if(s[i])lx[i]-=a;
		if(t[i])ly[i]+=a;
	}
}
void KM(){
	for(int i=1;i<=n;++i){
		f[i]=lx[i]=ly[i]=0;
		for(int j=1;j<=n;++j)
			lx[i]=max(lx[i],w[i][j]);
	}
	for(int i=1;i<=n;++i){
		while(1){
			for(int j=1;j<=n;++j)
				s[j]=t[j]=0;
			if(Find(i))break;
			else Upd();
		}
	}
}
void Solve(){
	for(int i=1;i<n;++i)
		printf("%d ",lx[n]);
	printf("%d\n",lx[n]);
	for(int i=1;i<n;++i)
		printf("%d ",ly[i]);
	printf("%d\n",ly[n]);
	int sum=0;
	for(int i=1;i<=n;++i) sum+=(lx[i]+ly[i]);
	printf("%d\n",sum);
}
void Read(){
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j){
			scanf("%d",&w[i][j]);
		}
	}
}
void Init(){
	memset(s,0,sizeof s);
	memset(t,0,sizeof t);
	memset(w,0,sizeof w);
	memset(lx,0,sizeof lx);
	memset(ly,0,sizeof ly);
	memset(f,0,sizeof f);

}
int main(){
//	freopen("1.in","r",stdin);
	while(scanf("%d",&n)!=EOF){
		Init();
		Read();
		KM();
		Solve();
	}
	return 0;
}

Golden Tiger Claw (KM算法)

标签:except   note   solution   this   where   careful   start   sam   case   

原文地址:https://www.cnblogs.com/Lour688/p/12870620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!