poj 3686 The Windy's

来源:互联网 发布:淘宝客推广能赚钱吗 编辑:程序博客网 时间:2024/05/16 05:46
The Windy's
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 5111 Accepted: 2135

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

33 4100 100 100 199 99 99 198 98 98 13 41 100 100 10099 1 99 9998 98 1 983 41 100 100 1001 99 99 9998 1 98 98

Sample Output

2.0000001.0000001.333333

Source

POJ Founder Monthly Contest – 2008.08.31, windy7926778

提示

题意:

温迪是世界上著名的玩具生厂商,拥有m(1<=m<=50)个车间,每个车间的生产都是独立的,不能两个产品同时生产,但可以等一个产品完成以后再生产一个,有n(1<=n<=50)个产品需要生产,给出每件产品在每个车间生产所花费的时间,求出最小花费时间的平均值。

思路:

因为可以有两个产品在一个车间生产的情况,其中设一个产品花费的时间为t1,若另一个产品也要在这个车间生产,花费时间为t2,花费总时间为t1+(t1+t2),因为花费时间=生产时间+等待时间,那么我们可以再建边。

以这个为例子,第二个产品和这个车间再建一条边,权值为t1+t2。假设所有的产品都在同一个车间进行,花费总时间为t1*n+t2*(n-1)+......

以此类推,套上KM模板即可。

示例程序

O(n^4)算法(貌似更快......):

Source CodeProblem: 3686Code Length: 2140BMemory: 924KTime: 16MSLanguage: GCCResult: Accepted#include <stdio.h>#include <string.h>#define MIN -1e9+7int map[2500][2500],lx[2500],ly[2500],sx[2500],sy[2500],match[2500];int path(int pos,int m){    int i;    sx[pos]=1;    for(i=0;m>i;i++)    {        if(sy[i]==0&&lx[pos]+ly[i]==map[pos][i])        {            sy[i]=1;            if(match[i]==-1||path(match[i],m)==1)            {                match[i]=pos;                return 1;            }        }    }    return 0;}double km(int n,int m){    int i,i1,i2,max,sum=0;    memset(lx,0,sizeof(lx));    memset(ly,0,sizeof(ly));    memset(match,-1,sizeof(match));    for(i=0;n>i;i++)    {        while(1)        {            memset(sx,0,sizeof(sx));            memset(sy,0,sizeof(sy));            if(path(i,m)==1)            {                break;            }            max=-MIN;            for(i1=0;n>i1;i1++)            {                if(sx[i1]==1)                {                    for(i2=0;m>i2;i2++)                    {                        if(sy[i2]==0&&max>lx[i1]+ly[i2]-map[i1][i2])                        {                            max=lx[i1]+ly[i2]-map[i1][i2];                        }                    }                }            }            for(i1=0;n>i1;i1++)            {                if(sx[i1]==1)                {                    lx[i1]=lx[i1]-max;                }            }            for(i1=0;m>i1;i1++)            {                if(sy[i1]==1)                {                    ly[i1]=ly[i1]+max;                }            }        }    }    for(i=0;m>i;i++)    {        if(match[i]!=-1)        {            sum=sum-map[match[i]][i];        }    }    return sum*1.0/n;}int main(){    int t,i,i1,i2,i3,n,m,x;    scanf("%d",&t);    for(i=1;t>=i;i++)    {        scanf("%d %d",&n,&m);        for(i1=0;n>i1;i1++)        {            for(i2=0;m>i2;i2++)            {                scanf("%d",&x);                for(i3=0;n>i3;i3++)//每件产品对于每个车间都建n个边                {                    map[i1][i2+(i3*m)]=-x*(i3+1);                }            }        }        printf("%f\n",km(n,m*n));    }    return 0;}

O(n^3)算法:

Source CodeProblem: 3686Code Length: 2293BMemory: 932KTime: 32MSLanguage: GCCResult: Accepted#include <stdio.h>#include <string.h>#define MIN -1e9+7int map[2500][2500],lx[2500],ly[2500],sx[2500],sy[2500],match[2500],slack[2500];int path(int pos,int m){    int i,t;    sx[pos]=1;    for(i=0;m>i;i++)    {        if(sy[i]==0)        {            t=lx[pos]+ly[i]-map[pos][i];            if(t==0)            {                sy[i]=1;                if(match[i]==-1||path(match[i],m)==1)                {                    match[i]=pos;                    return 1;                }            }            else if(slack[i]>t)            {                slack[i]=t;            }        }    }    return 0;}double km(int n,int m){    int i,i1,i2,sum=0,max;    memset(lx,0,sizeof(lx));    memset(ly,0,sizeof(ly));    memset(match,-1,sizeof(match));    for(i=0;n>i;i++)    {        for(i1=0;m>i1;i1++)        {            slack[i1]=-MIN;        }        while(1)        {            memset(sx,0,sizeof(sx));            memset(sy,0,sizeof(sy));            if(path(i,m)==1)            {                break;            }            max=-MIN;            for(i1=0;m>i1;i1++)            {                if(sy[i1]==0&&max>slack[i1])                {                    max=slack[i1];                }            }            for(i1=0;n>i1;i1++)            {                if(sx[i1]==1)                {                    lx[i1]=lx[i1]-max;                }            }            for(i1=0;m>i1;i1++)            {                if(sy[i1]==1)                {                    ly[i1]=ly[i1]+max;                }                else                {                    slack[i1]=slack[i1]-max;                }            }        }    }    for(i=0;m>i;i++)    {        if(match[i]!=-1)        {            sum=sum-map[match[i]][i];        }    }    return sum*1.0/n;}int main(){    int t,i,i1,i2,i3,n,m,x;    scanf("%d",&t);    for(i=1;t>=i;i++)    {        scanf("%d %d",&n,&m);        for(i1=0;n>i1;i1++)        {            for(i2=0;m>i2;i2++)            {                scanf("%d",&x);                for(i3=0;n>i3;i3++)//每件产品对于每个车间都建n个边                {                    map[i1][i2+(i3*m)]=-x*(i3+1);                }            }        }        printf("%f\n",km(n,m*n));    }    return 0;}

0 0