###### The Windy's - POJ 3686 KM算法

The Windy's
 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4408 Accepted: 1874

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

AC代码如下：

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int T,t,n,m,w[60][2600],cost[60][60],nx,ny,INF=1e9;
bool visx[60],visy[2600];
bool Find(int x)
{
int y,t;
visx[x]=1;
for(y=1;y<=ny;y++)
if(!visy[y])
{
t=lx[x]+ly[y]-w[x][y];
if(t==0)
{
visy[y]=1;
{
return 1;
}
}
else if(slack[y]>t)
slack[y]=t;
}
return false;
}
int KM()
{
int i,j,k,x,y,d,ans=0;
memset(ly,0,sizeof(ly));
for(i=1;i<=nx;i++)
{
lx[i]=-INF;
for(j=1;j<=ny;j++)
lx[i]=max(lx[i],w[i][j]);
}
for(i=1;i<=nx;i++)
{
for(j=1;j<=ny;j++)
slack[j]=INF;
while(true)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(Find(i))
break;
d=INF;
for(j=1;j<=ny;j++)
if(!visy[j] && d>slack[j])
d=slack[j];
for(j=1;j<=nx;j++)
if(visx[j])
lx[j]-=d;
for(j=1;j<=ny;j++)
if(visy[j])
ly[j]+=d;
else
slack[j]-=d;
}
}
for(j=1;j<=ny;j++)
return ans;
}
int main()
{
int i,j,k,ans;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&cost[i][j]);

nx=n;ny=n*m;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=m;k++)
w[i][(j-1)*m+k]=-cost[i][k]*j;
ans=KM();
printf("%.6f\n",-1.0*ans/n);
}
}


#### poj 3686The Windy's KM算法经典题

2015-12-08 16:41:12

#### POJ 3686 The Windy's

2014-02-16 19:40:45

#### POJ3686 The Windy's 神奇的拆点

2016-08-09 21:18:51

#### [ACM] POJ 3686 The Windy's (二分图最小权匹配，KM算法，特殊建图）

2014-11-01 14:02:17

#### km算法------poj3686

2016-02-15 19:03:27

#### poj 3686 The Windy's （KM算法）

2017-04-12 07:00:18

#### POJ 3686 The Windy's KM算法

2012-08-11 16:20:59

#### 【POJ 3686】The Windy's（KM算法）

2016-01-28 08:59:24

#### 【POJ 3686】【最小费用最大流或者KM算法 指派问题变形（需要拆点）】The Windy's n个玩具指派给m个工厂生产

2016-09-16 15:29:21

#### Poj 3686 The Windy's【KM匹配】

2016-09-26 19:22:15

## 不良信息举报

The Windy's - POJ 3686 KM算法