POJ 3686 The Windy's 二分图最差匹配

The Windy's
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3274   Accepted: 1395

Description

The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The manager knows that every order will take different amount of hours in different workshops. More precisely, the i-th order will take Zij hours if the toys are making in the j-th workshop. Moreover, each order's work must be wholly completed in the same workshop. And a workshop can not switch to another order until it has finished the previous one. The switch does not cost any time.

The manager wants to minimize the average of the finishing time of the N orders. Can you help him?

Input

The first line of input is the number of test case. The first line of each test case contains two integers, N and M (1 ≤ N,M ≤ 50). The next N lines each contain M integers, describing the matrix Zij (1 ≤ Zij ≤ 100,000) There is a blank line before each test case.

Output

For each test case output the answer on a single line. The result should be rounded to six decimal places.

Sample Input

3

3 4
100 100 100 1
99 99 99 1
98 98 98 1

3 4
1 100 100 100
99 1 99 99
98 98 1 98

3 4
1 100 100 100
1 99 99 99
98 1 98 98

Sample Output

2.000000
1.000000
1.333333

代码:
// 最差匹配名字是我自己叫的= = 
//搞了一个早上 终于知道错哪里了
//本来以为可以把最优匹配方法初始条件改了就可以
//但是调了好久 样例都没过= =
// 后来看了大量博客才知道哪里错了= = 
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
#define M 51
#define INF 23759827
int map[M][M*M] ;
int mark[M*M] , x[M] , y[M*M];
bool  vx[M] , vy[M*M];
int n , m , ans , Min ;
bool find( int xx )
{
	vx[xx] = 1 ;
	for( int i = 1 ; i <= n * m ;i++)
		if( !vy[i] && x[xx] + y[i] == map[xx][i] )
	{
		vy[i] = 1 ;
		if( mark[i] == -1 || find( mark[i] ) )
		{
			mark[i] = xx ;
			return 1 ;
			
		}
	}
	return 0 ;
}
void up()
{
	int i , j , a = INF ;
	for(  i = 1; i <= n ;i++)
		if( vx[i])
		for( j = 1; j <= n * m ; j++)
			if( !vy[j] )
		{
			  a=(map[i][j]-x[i]-y[j])<a?(map[i][j]-x[i]-y[j]):a;
		}
			//cout <<a <<endl ;
		for( i = 1; i <= n ;i++)if(vx[i])
		{
			x[i] += a ;// 与最优匹配相反
		}
		for( j = 1; j <= n * m ; j++)if(vy[j])
		{
			y[j] -= a ;// 我会说 a 算出来是负的= = 
		}
}
void insert()
{   
	for( int i = 1 ; i <= n ;i++){
		x[i] = INF ; // 特点
	}
}
void KM()
{   
	insert( ) ;
	memset( mark , -1 , sizeof(mark) ) ;
	memset( y , 0 , sizeof(y) ) ;
	int i , j ;
	for( i = 1; i <= n ;i++){
		while( 1 )
		{  
			memset( vx , 0 , sizeof(vx) );
			memset( vy , 0 ,sizeof(vy) ) ;
			if( find(i) ) break ;
			else up( ) ;
		}
	}
		for( i = 1 ; i <= n * m ; i++){
			if( mark[i] != -1 )
			ans += map[mark[i]][i] ;
		}
}
int main()
{
	int i , j , T ;
	cin >> T ;
	while(T--)
	{ 
		scanf( "%d%d" , &n ,&m ) ;
		memset( map , 0 , sizeof(map) ) ;
		for( i = 1; i <= n ;i++)
			for( j = 1; j <= m ;j++)
				scanf( "%d" , &map[i][j] ) ;
		for( i = 1; i <= n ;i++)
			for( int k = 2 ; k <= n ;k++)// 这构图太巧妙了 膜拜
				for( j = 1; j <= m ;j++)
					map[i][(k-1)*m+j] = k * map[i][j] ;
		ans = 0 ;
		 KM() ;
		printf( "%.6lf\n" , ans*1.0 / n) ; 
	}
} 

  

原文地址:https://www.cnblogs.com/20120125llcai/p/3052064.html