Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5048 | Accepted: 2114 |
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
Source
题目大意:预定了N个玩具,交付给M个工厂进行加工,J号工厂加工I号玩具需要Zi,j时间,每个玩具都应该完全在某一个工厂内加工完,玩具的制作顺序是可以任意的,但是每个工厂在完全加工好一个玩具前,都不能处理别的订货单,请问加工完所有玩具的平均时间的最小值。
思路:
1、我们假设只有一个工厂,有N个预定的玩具,如果我们按照a1,a2,a3,..........an的顺序处理这些玩具,对应需要的时间T=a1+(a1+a2)+(a1+a2+a3)+(a1+a2+a3+a4)+..............,那么将其整理出来:T=n*a1+(n-1)*a2+(n-2)*a3+.............
2、那么从上述式子当中就可以发现,当只有一个工厂的时候,为了最小化总时间,应该从花费时间较少的玩具开始加工。那么当工厂数不为1的时候怎样处理呢?
①T=n*a1+(n-1)*a2+(n-2)*a3+.............可以表示为一个工厂处理n个玩具,那么我们也可以看成一共有n个工厂,每个工厂只能加工一个玩具。
②那么这样有n个工厂,只不过其需要花费1倍到N倍的时间。这样来想的话,那么问题变成了简单的KM匹配问题了。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[55][55];
int w[55*55][55*55];
int lx[55*55];
int ly[55*55];
int vx[55*55];
int vy[55*55];
int match[55*55];
int n,m,low;
int find(int u)
{
vx[u]=1;
for(int i=0;i<n*m;i++)
{
if(vy[i]==1)continue;
int tmpp=lx[u]+ly[i]-w[u][i];
if(tmpp==0)
{
vy[i]=1;
if(match[i]==-1||find(match[i]))
{
match[i]=u;
return 1;
}
}
else if(tmpp<low)low=tmpp;
}
return 0;
}
void KM()
{
memset(match,-1,sizeof(match));
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
for(int i=0;i<n;i++)
{
for(int j=0;j<n*m;j++)
{
lx[i]=max(lx[i],w[i][j]);
}
}
for(int i=0;i<n;i++)
{
while(1)
{
memset(vx,0,sizeof(vx));
memset(vy,0,sizeof(vy));
low=0x3f3f3f3f;
if(find(i))break;
for(int j=0;j<n*m;j++)
{
if(vx[j])lx[j]-=low;
if(vy[j])ly[j]+=low;
}
}
}
double sum=0;
for(int i=0;i<n*m;i++)
{
if(match[i]==-1)continue;
sum+=w[match[i]][i];
}
printf("%.6f\n",-sum/n);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
for(int k=0;k<n;k++)
{
w[i][j+k*m]=-(a[i][j]*(k+1));
}
}
}
KM();
}
}