2018.06.27The Windy's(费用流)

The Windy’s
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6003 Accepted: 2484
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

POJ Founder Monthly Contest – 2008.08.31, windy7926778

此题题意简述一下就是说有nnn个任务,mmm个工厂,每个任务在每个工厂完成所需时间不同,让你求出完成所有任务的结束时间总和的最大值。

那我们冷静分析一下:,这显然是一道网络流的题,既然每个任务都只能被完成一次,那么我们可以建立源点sss,向nnn个任务每个都连一条容量为111,权值为000的边来保证每个任务都被完成且只被完成一次。

该题的重头戏来了:我们应该如何建图来表示每个任务在不同工厂的花费呢?理性思考后我们发现,我们设第aaa个工厂中要完成的第1−>k1->k1>k个任务为A1,A2,…AkA_1,A_2,…A_kA1,A2,Ak,那么第111个任务对答案的贡献为A1∗kA_1*kA1k,第222个任务对答案的贡献为A2∗(k−1)A_2*(k-1)A2(k1),以此类推下去。这样的话,我们只需把每个工厂kkk拆成nnn个点,第iii个点表示这个工厂中与第iii个任务匹配的点,第jjj个任务连向它的边容量仍然是111,费用是w(i,k)∗jw(i,k)*jw(i,k)j,然后跑一发最小费用最大流就能搞定了。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
#define N 10005
#define M 1000000 
using namespace std;
inline long long read(){
	long long ans=0,w=1;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-')w=-1;
		ch=getchar();
	}
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
	return ans*w;
}
struct Node{int v,next,c,w;}e[M];
int first[N],d[N],pos[N],pred[N],flow[N],cnt,T,s,t,n,m;
bool in[N];
inline void add(int u,int v,int c,int w){
	e[++cnt].v=v;
	e[cnt].next=first[u];
	e[cnt].w=w;
	e[cnt].c=c;
	first[u]=cnt;
	e[++cnt].v=u;
	e[cnt].next=first[v];
	e[cnt].w=-w;
	e[cnt].c=0;
	first[v]=cnt;
}
inline bool spfa(){
	queue<int>q;
	memset(d,inf,sizeof(d));
	memset(flow,inf,sizeof(flow));
	memset(in,false,sizeof(in));
	q.push(s),d[s]=0,in[s]=true,pred[t]=-1;
	while(!q.empty()){
		int x=q.front();
		q.pop(),in[x]=false;
		for(int i=first[x];i!=-1;i=e[i].next){
			int v=e[i].v;
			if(e[i].c>0&&d[v]>d[x]+e[i].w){
				d[v]=d[x]+e[i].w,pred[v]=x,pos[v]=i,flow[v]=min(flow[x],e[i].c);
				if(!in[v])in[v]=true,q.push(v);
			}
		}
	}
	return pred[t]!=-1;
}
inline void solve(){
	int maxw=0;
	while(spfa()){
		maxw+=d[t];
		int now=t;
		while(now!=s){
			e[pos[now]].c-=flow[t];
			e[pos[now]^1].c+=flow[t];
			now=pred[now];
		}
	}
	printf("%.6f\n",maxw*1.0/n);
}
int main(){
	T=read();
	while(T--){
		n=read(),m=read(),s=0,t=n+n*m+1;
		memset(first,-1,sizeof(first));
		memset(e,0,sizeof(e));
		cnt=-1;
		for(int i=1;i<=n;++i)add(s,i,1,0);
		for(int i=1;i<=n;++i)
			for(int j=1;j<=m;++j){
				int v=read();
				for(int k=1;k<=n;++k)add(i,j*n+k,1,v*k);
			}
		for(int i=n+1;i<=n*m+n;++i)add(i,t,1,0);
		solve();
	}
	return 0;
}

转载于:https://www.cnblogs.com/ldxcaicai/p/9738568.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值