POJ 3686 The Windy's KM模版+拆点建图

46 篇文章 0 订阅
19 篇文章 0 订阅

题目的意思是:有N个订单和M个机器,给出第i个订单在第j个机器完成的时间Mij,每台机器同一时刻只能处理一个订单,机器必须完整地完成一个订单后才能接着完成下一个订单。问N个订单完成时间的平均值最少为多少。

这里注意建图的时候的权值问题,因为物品被生产出来之后越早生产出来的等的时间越长:可以列一个式子:∑s = (s1)+(s1+s2)+.......+(s1+s2+...+sk)。所以可以合并一下:∑s = k*s1+(k-1)*s2+......+sk;(k代表个数)。所以需要拆一下点,所以建的时候j是从1到n*m个。然后就是套KM模版找到最优的结果。

The Windy's
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3618 Accepted: 1553

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 100100;
using namespace std;


int link[maxn], w[110][maxn];
int lx[maxn], ly[maxn];
int slack[maxn];
bool vtx[maxn], vty[maxn], vt[maxn];
int match[maxn];
int n, m;
int str[110][110];

bool dfs(int i)
{
    int j;
    vtx[i] = true;

    for(j = 1; j <= m; j++)
    {
        if(vty[j]) continue;
        int tmp = lx[i] + ly[j] - w[i][j];
        if(tmp == 0)
        {
            vty[j] = true;
            if(dfs(link[j]) || link[j] == -1)
            {
                link[j] = i;
                return true;
            }
        }
        else
            slack[j] = min(tmp, slack[j]);
    }
    return false;
}

int KM()
{
    for(int i = 1; i <= n; i++)
    {
        lx[i] = -INF;
        for(int j = 1 ; j <= m; j++)
        {
            lx[i] = max(lx[i], w[i][j]);
        }
    }
    memset(link , -1 , sizeof(link));
    memset(ly , 0 , sizeof(ly));
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
            slack[j] = INF;
        while(1)
        {
            memset(vtx , false , sizeof(vtx));
            memset(vty , false , sizeof(vty));
            if(dfs(i))
                break;
            int d = INF;
            for(int j = 1; j <= m; j++)
            {
                if(!vty[j] && d > slack[j])
                    d = slack[j];
            }
            if(d == INF) return -1;
            for(int j = 1; j <= n; j++)
                if(vtx[j])
                    lx[j] -= d;
            for(int j = 1; j <= m; j++)
            {
                if(vty[j])
                    ly[j] += d;
                else
                    slack[j] -= d;
            }

        }
    }
    int sum = 0;
    for(int i = 1; i <= m; i++)
    {
        if(link[i] > -1)
            sum -= w[link[i]][i];
    }
    return sum;
}

int main()
{
    int t;
    cin >>t;
    while(t--)
    {
        cin >>n>>m;
        memset(w , 0 , sizeof(w));
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                cin >>str[i][j];
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                for(int k = 1; k <= n; k++)
                    w[i][(j-1)*n+k] = -str[i][j]*k;
        m = n*m;
        cout<<fixed<<setprecision(6)<<1.0*KM()/n<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值