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*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(费用流)的更多相关文章
- [poj3686]The Windy's(费用流)
题目大意: 解题关键:指派问题,待更. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- 2018.10.15 loj#6010. 「网络流 24 题」数字梯形(费用流)
传送门 费用流经典题. 按照题目要求建边. 为了方便我将所有格子拆点,三种情况下容量分别为111,infinfinf,infinfinf,费用都为validi,jval_{id_{i,j}}valid ...
- 2018.10.15 loj#6013. 「网络流 24 题」负载平衡(费用流)
传送门 费用流sb题. 直接从sss向每个点连边,容量为现有物品量. 然后从ttt向每个点连边,容量为最后库存量. 由于两个点之间可以互相任意运送物品,因此相邻的直接连infinfinf的边就行了. ...
- 2018.10.14 loj#6012. 「网络流 24 题」分配问题(费用流)
传送门 费用流水题. 依然是照着题意模拟建边就行了. 为了练板子又重新写了一遍费用流. 代码: #include<bits/stdc++.h> #define N 305 #define ...
- 2018.10.14 loj#6011. 「网络流 24 题」运输问题(费用流)
传送门 费用流入门题. 直接按照题意模拟. 把货物的数量当做容量建边. 然后跑一次最小费用流和最大费用流就行了. 代码: #include<bits/stdc++.h> #define N ...
- 2018.10.13 bzoj1834: [ZJOI2010]network 网络扩容(最大流+费用流)
传送门 网络流水题啊. 第一问直接放心跑最大流(本来还以为有什么tricktricktrick). 第二问就直接把原来的边(u,v,c,w)(u,v,c,w)(u,v,c,w)变成(u,v,c,0)( ...
- 2018.10.13 bzoj1070: [SCOI2007]修车(费用流)
传送门 费用流经典题目. 自我感觉跟TheWindy′sThe Windy'sTheWindy′s很像. 利用费用提前计算的思想来建图就行了. 代码: #include<bits/stdc++. ...
- POJ 3686 The Windy's (费用流)
[题目链接] http://poj.org/problem?id=3686 [题目大意] 每个工厂对于每种玩具的加工时间都是不同的, 并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均 ...
随机推荐
- TOJ4413: IP address
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4413 时间限制(普通/Java): ...
- PTA 7-33 地下迷宫探索(深搜输出路径)
地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时,真心钦佩他们的聪明才智. ...
- VTP
VTP VLAN中继协议(Vlan Trunking Protocol),是CISCO专用协议.VTP负责在VTP域内同步VLAN信息,这样就不必在每个交换机上配置相同的VLAN信息.VTP还提供一种 ...
- ARM板移植udev-126
下载udev-126.tar.xz 下载的网址为: https://mirrors.edge.kernel.org/pub/linux/utils/kernel/hotplug/ 解压文件并且编译 # ...
- shell脚本实例总结
1.判断文件夹是否存在 #!/bin/sh workspace="/home/web/mall" #如果文件夹不存在 if [ ! -d ${workspace} ]; then ...
- ES5/6/7
ECMAScript(js语言规范) ###ES5 1. 严格模式 运行模式: 正常(混杂)模式与严格模式 应用上严格模式: ‘strict mode’ 2.JSON对象 * JSON.strin ...
- sourceTree git的一些命令
经常使用的三个命令 1.添加修改过的文件到缓冲区 git add. 2.commit到本地 git commit -am ' 更改描述' 3.如果是多人开发的话,中间可能会有别人先提交的这是就需要先把 ...
- MVC报错:找到多个与名为“Home”的控制器匹配的类型。
错误原因是:在根目录中的Controller中有HomeController,而在Areas中也有一个HomeController,只是他们的命名空间不一样. 这样的话,只需要在对应的路由注册中加入命 ...
- .NET垃圾回收机制
在.net 编程环境中,系统的资源分为托管资源和非托管资源. 对于托管的资源的回收工作,是不需要人工干预回收的,而且你也无法干预他们的回收,所能够做的只是了解.net CLR如何做这些操作.也就是说 ...
- 1.maven安装配置
这段时间在做项目构建管理方面的工作,以前很多项目都是通过ant去构建的,虽然很早就接触过mavan,但是从没有系统的去学习过, 现在项目需要用maven来构建,我结合自己的心得整理一下放在博客上作为自 ...