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. TOJ 3973 Maze Again && TOJ 3128 简单版贪吃蛇

    TOJ3973传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3973 时间限制(普通 ...

  2. unity填色绘画游戏Drawing Coloring Extra Edition

    . 下载地址: https://item.taobao.com/item.htm?spm=0.7095261.0.0.2e611debLdF3mf&id=576153069662

  3. jquery一些容易混淆的

    1.父级元素.append子集元素 == 子集元素.appendTo父级元素 2.父级元素.prepend子集元素 == 子集元素.prependTo父级元素 3.同辈1.insertBefore同辈 ...

  4. 破解myeclipse 2014

    用网上的教程的确可以,但是他似乎写的有点少.....试了很多次,说说他少的: http://jingyan.baidu.com/article/fdbd42771039bfb89e3f4838.htm ...

  5. OC 线程操作1 - pthread

    #import "ViewController.h" #import <pthread.h> //1.需要包含这个头文件 @interface ViewControll ...

  6. 将之前的Power idea公司的数据按照下图所示的格式在屏幕上显示出来。

    之前的文章 示例代码如下 assume cs:codesg ;将整个data段看作是一个数组,长度一共为 ;21*4+21*4+2*21=168+42=210字节 data segment db ' ...

  7. Swift 错误记录

    最近几天开始学习Swift,作为一个从 OC 转到 Swift 的程序员,我一直以为很简单.但是现在学习一个星期之后,发现问题还是蛮多的! 最大的问题就是 太特么 不习惯了!!!!!!!!!! 好吧, ...

  8. datepicker动态初始化

    datepicker 初始化动态表单的input,需要调用jquery的on方法来给未来元素初始化. //对动态添加的时间文本框进行动态初始化 $('table').on("focus&qu ...

  9. Struts2框架的数据封装一之属性封装(属性封装的第一种方式:对参数进行封装)

    request带着参数来,aciton对其进行处理.在学习action之前,使用的是servlet对request进行处理.request请求时会带有参数,所以我们要对这些参数进行封装. 1. 为什么 ...

  10. c#dev tabcontrol 切换页面时注意的问题

    先加一个代码 public void SetXtraTabPageVisible(DevExpress.XtraTab.XtraTabControl xtraTabControl, bool iIsV ...