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*kA1​∗k,第222个任务对答案的贡献为A2∗(k−1)A_2*(k-1)A2​∗(k−1),以此类推下去。这样的话,我们只需把每个工厂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;
}

2018.06.27The Windy's(费用流)的更多相关文章

  1. [poj3686]The Windy's(费用流)

    题目大意: 解题关键:指派问题,待更. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  2. 2018.06.27"Shortest" pair of paths(费用流)

    "Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...

  3. 2018.10.15 loj#6010. 「网络流 24 题」数字梯形(费用流)

    传送门 费用流经典题. 按照题目要求建边. 为了方便我将所有格子拆点,三种情况下容量分别为111,infinfinf,infinfinf,费用都为validi,jval_{id_{i,j}}valid ...

  4. 2018.10.15 loj#6013. 「网络流 24 题」负载平衡(费用流)

    传送门 费用流sb题. 直接从sss向每个点连边,容量为现有物品量. 然后从ttt向每个点连边,容量为最后库存量. 由于两个点之间可以互相任意运送物品,因此相邻的直接连infinfinf的边就行了. ...

  5. 2018.10.14 loj#6012. 「网络流 24 题」分配问题(费用流)

    传送门 费用流水题. 依然是照着题意模拟建边就行了. 为了练板子又重新写了一遍费用流. 代码: #include<bits/stdc++.h> #define N 305 #define ...

  6. 2018.10.14 loj#6011. 「网络流 24 题」运输问题(费用流)

    传送门 费用流入门题. 直接按照题意模拟. 把货物的数量当做容量建边. 然后跑一次最小费用流和最大费用流就行了. 代码: #include<bits/stdc++.h> #define N ...

  7. 2018.10.13 bzoj1834: [ZJOI2010]network 网络扩容(最大流+费用流)

    传送门 网络流水题啊. 第一问直接放心跑最大流(本来还以为有什么tricktricktrick). 第二问就直接把原来的边(u,v,c,w)(u,v,c,w)(u,v,c,w)变成(u,v,c,0)( ...

  8. 2018.10.13 bzoj1070: [SCOI2007]修车(费用流)

    传送门 费用流经典题目. 自我感觉跟TheWindy′sThe Windy'sTheWindy′s很像. 利用费用提前计算的思想来建图就行了. 代码: #include<bits/stdc++. ...

  9. POJ 3686 The Windy's (费用流)

    [题目链接] http://poj.org/problem?id=3686 [题目大意] 每个工厂对于每种玩具的加工时间都是不同的, 并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均 ...

随机推荐

  1. [LeetCode_94] Binary Tree Inorder Traversal

    题目链接 https://leetcode.com/problems/binary-tree-inorder-traversal/ 题意 二叉树的中序遍历 思路 中序遍历:即"左中右&quo ...

  2. 【C++ const_cast强制转换】

  3. 43-将javaweb项目部署到Linux服务器

    这是第二次弄了,感觉由于上次积累了点资源,这次要少走很多弯路了,再次记录下来吧. 第一次的记录:将本地的javaweb项目部署到Linux服务器的一般操作 1. 在Linux上建立数据库,我是将本地的 ...

  4. 36-2018 蓝桥杯Java B组试题及答案

    1:第几天2000年的1月1日,是那一年的第1天.那么,2000年的5月4日,是那一年的第几天? 注意:需要提交的是一个整数,不要填写任何多余内容. 用excel算,答案125. 2.标题:方格计数 ...

  5. linux下rsync+inotify实现服务器之间文件实时同步

    先介绍一下rsync与inotify. 1.rsync 与传统的cp.tar备份方式相比,rsync具有安全性高.备份迅速.支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例 ...

  6. Linux后台运行进程

    方式一: /bin/bash /var/www/html/crontab.sh & 缺点:终端关闭的话,进程也会关闭. 方式二: nohup /bin/bash /var/www/html/c ...

  7. linux ubuntu设置root用户初始密码

    输入 sudo passwd 命令,输入一般用户密码并设定root用户密码. 设定root密码成功后,输入 su 命令,并输入刚才设定的root密码,就可以切换成root了. 提示符$代表一般用户,提 ...

  8. ABP框架使用Mysql数据库

    参考文档:https://github.com/ABPFrameWorkGroup/AbpDocument2Chinese/blob/master/Markdown/Abp/9.4ABP%E5%9F% ...

  9. PAT 甲级1001 A+B Format (20)(C++ -思路)

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  10. 如何快速学好Shell脚本?

    Shell 语言作为类 Unix 系统的原生脚本,有着非常实用的价值.但对于很多刚刚接触 Shell 脚本的同学来说,搞懂 Shell 语言的语法却是一件非常困难的事情.甚至有人吐槽,或许没有谁能清楚 ...