码迷,mamicode.com
首页 > Windows程序 > 详细

POJ 3686 The Windy's 最小权值匹配

时间:2014-05-10 09:40:27      阅读:546      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

The Windy‘s
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3788   Accepted: 1630

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台机器去做,每台机器一次只能做一个任务,而且每个任务必须经过这m台机器才能完成,先给你每个任务在每台机器的时间,问最少的平均时间是多少。
完成所有的任务总时间是实际时间+等待时间,设完成1,2,3....n的时间分别为t1,t2,t3...tn,那么总时间就是t=t1+(t1+t2)+(t1+t2+t3)+......+(t1+t2+...+tn)=t1*n+t2*(n-1)+t3*(n-2)+tn
第k个任务在倒数第j台机器处理tk*j。
每个机器可以处理n个任务,将每个机器拆成n个点,1~n分别代表某个任务在这个机器上倒数第几个被加工的,对于每个任务i,机器j中拆的每个点k,连接一条-map[i][j]*k权值的边。
//36044K	579MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 3007
#define inf 0x3f3f3f
using namespace std;
int g[M][M],map[M][M];
int lx[M],ly[M];
int slack[M],match[M];
bool visx[M],visy[M];
int n,m;
void build()
{
    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]=-map[i][j]*k;
    m=n*m;
}
bool dfs(int cur)
{
    visx[cur]=true;
    for(int y=1; y<=m; y++)
    {
        if(!visy[y]&&lx[cur]+ly[y]==g[cur][y])
        {
            visy[y]=true;
            if(match[y]==-1||dfs(match[y]))
            {
                match[y]=cur;
                return true;
            }
        }
    }
    return false;
}
int KM()
{
    memset(match,-1,sizeof(match));
    memset(ly,0,sizeof(ly));
    for(int i=1; i<=n; i++)
    {
        lx[i]=-inf;
        for(int j=1; j<=m; j++)
            lx[i]=max(lx[i],g[i][j]);
    }
    for(int x=1; x<=n; x++)
    {
        while(true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x))break;
            int d=inf;
            for(int j=1; j<=n; j++)
                if(visx[j])
                {
                    for(int k=1; k<=m; k++)
                    {
                        if(!visy[k]&&d>lx[j]+ly[k]-g[j][k])
                        {
                            d=lx[j]+ly[k]-g[j][k];
                        }
                    }
                }
            for(int i=1; i<=n; i++)
                if(visx[i])
                    lx[i]-=d;
            for(int i=1; i<=m; i++)
                if(visy[i])
                    ly[i]+=d;
        }
    }
    int result=0;
    for(int i=1; i<=m; i++)
    {
        if(match[i]!=-1&&g[match[i]][i]!=-inf)
            result+=g[match[i]][i];
    }
    return result;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(g,0,sizeof(g));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                scanf("%d",&map[i][j]);
        build();
        int ans=KM();
        printf("%.6f\n",-1.0*(double)ans/n);
    }
    return 0;
}


POJ 3686 The Windy's 最小权值匹配,布布扣,bubuko.com

POJ 3686 The Windy's 最小权值匹配

标签:des   style   blog   class   code   tar   

原文地址:http://blog.csdn.net/crescent__moon/article/details/24989439

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!