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 [题目大意] 每个工厂对于每种玩具的加工时间都是不同的, 并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均 ...
随机推荐
- Codeforces Beta Round #55 (Div. 2)
Codeforces Beta Round #55 (Div. 2) http://codeforces.com/contest/59 A #include<bits/stdc++.h> ...
- vue 路由参数变化,页面不更新的问题
监控$route 在vue项目中,假使我们在同一个路由下,只是改变路由后面的参数值,如果不监听路由参数值的变化,页面无数据刷新,需手动刷新浏览器,这样做就不是我们的预期效果. 举例:当前路由为 /p ...
- f5单台安装配置
1.对设备的管理口进行配置 管理口的默认设置 •IP 地址: 192.168.1.245/24 2.用户名和密码 1)初始密码: 图形界面用户名/密码:admin/admin:命令行用户名/密码:ro ...
- Python: 浅淡Python中的属性(property)
起源:项目过程中需要研究youtube_dl这个开源组件,翻阅其中对类的使用,对比c#及Delphi中实现,感觉Python属性机制挺有意思.区别与高级编程语言之单一入口,在类之属性这一方面,它随意的 ...
- 844. Backspace String Compare判断删除后的结果是否相等
[抄题]: Given two strings S and T, return if they are equal when both are typed into empty text editor ...
- jquery写tab切换,三行代码搞定
<script type="text/javascript"> $("button").on("click",function( ...
- linux小笔记
1. 安装go并设置环境变量 Add /usr/local/go/bin to the PATH environment variable. You can do this by adding thi ...
- devexpress WinForms MVVM
WinForms MVVM This section is dedicated to the Model-View-ViewModel (MVVM) architectural pattern. Yo ...
- istio-jaeger-spring boot调用链配置
istio-jaeger-spring boot调用链配置 虽然,istio ingress controller已经生成了jaeger 记录所需要的信息,但是多个分布式之间没法清晰记录相互之间的依赖 ...
- (转)JavaScript的压缩
JavaScript的压缩 (转自)http://blog.csdn.net/ybygjy/article/details/6995435 简述 如果非常着急,这块可以跳过直接从约束条件开始也行. J ...