poj 3686 The Windy's(最小权值和)

来源:互联网 发布:淘宝评价模版 编辑:程序博客网 时间:2024/05/24 06:49
The Windy's
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 3490 Accepted: 1496

Description

The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receivesN orders for toys. The manager knows that every order will take different amount of hours in different workshops. More precisely, thei-th order will take Zij hours if the toys are making in thej-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 matrixZij (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
题意:有n个工序,m台机器。每个工序都可以在m台机器中完成,不同的工序在不同的机器加工时间不同,机器只有在完成一个工序之后才能做下一个工序,工序的交接之间不需时间,现在问要完成全部n个工序,最小的平均完成时间是多少。
思路:如果多个工序都在同一台机器加工,这时一个工序的完成时间包括等待时间和加工时间,假设有k个工序在同一台机器加工,k个工序的加工时间分别为a1、a2、a3...ak,
那么这k个工序完成时间(包括等待时间和加工时间)分别为a1,a1+a2,a1+a2+a3,...,a1+a2+...+ak。即a1*k+a2*(k-1)+a3*(k-2)+...+ak。那么我们将每台机器拆成n个点,点k表示某工序是在该机器倒数第k个加工的,连一条边权值为-cost[i][j]*k。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll long long#define eps 1e-6using namespace std;const int maxn=3000;const int INF=1000000000;int G[55][maxn],slack[maxn],lx[55],ly[maxn];int match[maxn],cost[55][maxn];bool visx[55],visy[maxn];int n,m;bool find(int u){    visx[u]=true;    for(int i=1; i<=m; i++)    {        if(visy[i]) continue;        if(lx[u]+ly[i]==G[u][i])        {            visy[i]=true;            if(match[i]==-1||find(match[i]))            {                match[i]=u;                return true;            }        }        slack[i]=min(slack[i],lx[u]+ly[i]-G[u][i]);    }    return false;}int KM(){    memset(match,-1,sizeof(match));    for(int i=1; i<=n; i++) lx[i]=-INF;    memset(ly,0,sizeof(ly));    for(int i=1; i<=n; i++)        for(int j=1; j<=m; j++)            lx[i]=max(lx[i],G[i][j]);    for(int i=1; i<=n; i++)    {        for(int j=1; j<=m; j++) slack[j]=INF;        while(1)        {            memset(visx,false,sizeof(visx));            memset(visy,false,sizeof(visy));            if(find(i)) break;            else            {                int temp=INF;                for(int j=1; j<=m; j++)                    if(!visy[j]&&temp>slack[j]) temp=slack[j];                for(int j=1; j<=n; j++)                    if(visx[j]) lx[j]-=temp;                for(int j=1; j<=m; j++)                {                    if(visy[j]) ly[j]+=temp;                    else slack[j]-=temp;                }            }        }    }    int ans=0;    for(int i=1;i<=m;i++)    if(match[i]!=-1) ans+=G[match[i]][i];    return -ans;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=1; i<=n; i++)            for(int j=1; j<=m; j++)                scanf("%d",&cost[i][j]);        for(int i=1; i<=n; i++)            for(int j=1; j<=m; j++)                for(int k=1; k<=n; k++)                    G[i][(j-1)*n+k]=-cost[i][j]*k;        m*=n;        printf("%lf\n",KM()*1.0/n);    }    return 0;}