【POJ 3686】The Windy's(KM算法)

【POJ 3686】The Windy's(KM算法)


The Windy's
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4721 Accepted: 1987

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

Source


裸的KM求最小权加上拆点。

由于每个玩具对应每个机器可能会在n种时间下被加工,也就是可能是第1~n任何一个次序被加工

把每个机器拆出n个点 表示倒数第i个被加工,然后把每个玩具与这个点的边权变为cost[toy][0]*i

证明:假设某个机器加工玩具次序为 t1 t2 t3 t4 t5....tk

在这个机器上花费的总时间为 t1+(t1+t2)+(t1+t2+t3)+....+(t1+t2+t3+...+tk)

即为t1*k+t2*(k-1)+t3*(k-2)+....+tk-1*(2)+tk

即倒数第i个被加工 对于该机器的消耗时间贡献为t*i


建图想好了 就是一个裸的KM求最小权了


代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const double eps = 1e-8;

int mp[55][23333];
bool visy[23333];
bool visx[55];
int lx[55];
int ly[23333];
int slack[23333];
int link[23333];
int n,m;

bool cal(int x)
{
	visx[x] = 1;

	for(int y = 0; y < m; ++y)
	{
		if(visy[y]) continue;

		int t = lx[x]+ly[y]-mp[x][y];
		if(!t)
		{
			visy[y] = 1;
			if(link[y] == -1 || cal(link[y]))
			{
				link[y] = x;
				return 1;
			}
		}
		else slack[y] = min(slack[y],t);
	}
	return 0;
}

int KM()
{
	memset(link,-1,sizeof(link));

	for(int i = 0; i < n; ++i)
	{
		memset(slack,INF,sizeof(slack));

		while(1)
		{
			memset(visx,0,sizeof(visx));
			memset(visy,0,sizeof(visy));

			if(cal(i)) break;

			int d = INF;
			for(int j = 0; j < m; ++j)
				if(!visy[j]) d = min(d,slack[j]);

			for(int j = 0; j < n; ++j)
				if(visx[j]) lx[j] -= d;

			for(int j = 0; j < m; ++j)
				if(visy[j]) ly[j] += d;
				else slack[j] -= d;
		}
	}

	int ans = 0;
	for(int i = 0; i < m; ++i)
		if(link[i] != -1)
		{
			ans += mp[link[i]][i];
			//printf("%d-%d %d\n",link[i],i,-mp[link[i]][i]);
		}
	return -ans;
}

int main()
{
	int t;
	scanf("%d",&t);

	while(t--)
	{
		scanf("%d %d",&n,&m);

		memset(ly,0,sizeof(ly));
		memset(lx,-INF,sizeof(lx));
		m *= n;
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < m; j += n)
			{
				scanf("%d",&mp[i][j]);
				mp[i][j] = -mp[i][j];
				lx[i] = max(lx[i],mp[i][j]);

				for(int k = j+1; k < j+n; ++k)
					mp[i][k] = mp[i][k-1]+mp[i][j];
			}

		printf("%.6f\n",KM()*1.0/n);
	}

	return 0;
}






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值