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
题意:有n个物品,m个制造车间,每个物品在一个车间内制造有相应的时间,问所有商品从开始到制作完成的时间的和的平均值最小是多少。
思路:用KM算法,一边是n个物品,一边是n*m的矩阵,表示a个物品在第j个车间里是第倒数i个完成的。对应的权值是cost[a][j]*i,可以这么理解:它在倒数第i个完成后,对后面的i-1个物品等待其花费的时间加入这个权值当中。
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;
int lx[60],ly[2600],linky[2600],slack[2600];
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;
if(linky[y]==-1 || Find(linky[y]))
{
linky[y]=x;
return 1;
}
}
else if(slack[y]>t)
slack[y]=t;
}
return false;
}
int KM()
{
int i,j,k,x,y,d,ans=0;
memset(linky,-1,sizeof(linky));
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++)
if(linky[j]>-1)
ans+=w[linky[j]][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);
}
}