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

20 篇文章 0 订阅
The Windy's
Time Limit: 5000MS Memory Limit: 65536K
Total 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 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台机器中完成,不同的工序在不同的机器加工时间不同,机器只有在完成一个工序之后才能做下一个工序,工序的交接之间不需时间,现在问要完成全部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-6
using 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值