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

点击打开链接

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 最小权值匹配

时间: 2024-10-05 07:54:59

POJ 3686 The Windy's 最小权值匹配的相关文章

POJ 3686.The Windy&#39;s 最小费用最大流

The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5477   Accepted: 2285 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 ma

POJ-2195 Going Home---KM算法求最小权值匹配(存负边)

题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格需花费$1(即单位费用=单位距离),一间house只能入住一个man.现在要求所有的man都入住house,求最小费用. 思路: KM算法传送门: 理解篇    运用篇 每个man和house建立带权二分图,曼哈顿距离就是边的值,这里要求最小费用,也就是二分图最小权值匹配,但是KM算法求的是二分图最

POJ 2195 Going Home 【二分图最小权值匹配】

传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26151   Accepted: 13117 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit st

POJ 2404 Jogging Trails(最小权完美匹配)

[题目链接] http://poj.org/problem?id=2404 [题目大意] 给出一张图,求走遍所有的路径至少一次,并且回到出发点所需要走的最短路程 [题解] 如果图中所有点为偶点,那么一定存在欧拉回路, 否则一定存在偶数个奇点,将这些奇点取出构建新图, 任意两点之间的边权威原图中两点的最短距离, 用状压DP求最小权完美匹配,加上原图所有边权和就是答案. [代码] #include <cstdio> #include <algorithm> #include <c

POJ 3686 The Windy&#39;s【最小权匹配(神建图啊)】

大意:有n个任务m个机器,告诉你n*m的矩阵表示每个任务在每个机器上完成需要的时间 问所有任务完成的总时间最少?(比如第一个任务在第一分钟完成第二个任务在第二分钟完成   则总时间为1 + 2 = 3 分析: 该题自己做的时候没有思路 后来在网上搜题解,感觉建图真是太厉害了 假设最优情况下,个个任务需要的时间分别为a1, a2, a3, ……,an 那么总时间t = n * a1 + (n - 1) * a2 + ……+ 2 * an - 1 + an 也就是说只需要考虑系数就可以了 我们先假设

[ACM] POJ 3686 The Windy&#39;s (二分图最小权匹配,KM算法,特殊建图)

The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4158   Accepted: 1777 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 man

POJ 2253 Frogger(Dijkstra变形——最短路径最小权值)

题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' suns

poj 3686 The Windy&#39;s 二分图最小权和匹配KM

题意: 给n个玩具和m家工厂,每个玩具只能在一家工厂加工,给出每个玩具在每家工厂需要的加工时间,求这n个玩具完成时间平均值的最小值. 分析: 求最小和容易联系到二分图的最小权和匹配,这题的问题是n与m的大小关系是不确定的,也是就是说可能会有多个玩具到同一家工厂里加工的情况,这似乎与匹配的定义相矛盾.但仔细一想,如果多个玩具在同一工厂加工,他们的完成时间肯定是不一样的,所以讲w[i][j]=z 拆成w[i][0*m+j]=z,w[i][1*m+j]=2*z,w[i][2*m+j]=3*z.....

POJ 2195 Going Home(BFS+KM求最小权值)

Description: On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step