# The Windy's - POJ 3686 KM算法

276人阅读 评论(0)

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);
}
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：298232次
• 积分：9621
• 等级：
• 排名：第1917名
• 原创：696篇
• 转载：12篇
• 译文：0篇
• 评论：22条
阅读排行
最新评论