POJ 3686 The Windy's (费用流)
【题目链接】 http://poj.org/problem?id=3686
【题目大意】
每个工厂对于每种玩具的加工时间都是不同的,
并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均时间
【题解】
因为每个工厂加工一个零件在不同的时间是有不同代价的,
我们发现对于一个工厂在每次加工一个零件时候,时间要加上之前所有的零件的时间的条件
其实等价于对这个工厂加工的零件乘上1~N的不同系数。
那么我们将这个工厂对于时间进行拆点,对于费用乘上不同的系数,求一遍费用流即可
【代码】
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> P;
struct edge{int to,cap,cost,rev;};
const int MAX_V=10000;
int V,h[MAX_V],dist[MAX_V],prevv[MAX_V],preve[MAX_V];
vector<edge> G[MAX_V];
void add_edge(int from,int to,int cap,int cost){
G[from].push_back((edge){to,cap,cost,G[to].size()});
G[to].push_back((edge){from,0,-cost,G[from].size()-1});
}
int min_cost_flow(int s,int t,int f){
int res=0;
fill(h,h+V,0);
while(f>0){
priority_queue<P,vector<P>,greater<P> > que;
fill(dist,dist+V,INF);
dist[s]=0;
que.push(P(0,s));
while(!que.empty()){
P p=que.top(); que.pop();
int v=p.second;
if(dist[v]<p.first)continue;
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
prevv[e.to]=v;
preve[e.to]=i;
que.push(P(dist[e.to],e.to));
}
}
}
if(dist[t]==INF)return -1;
for(int v=0;v<V;v++)h[v]+=dist[v];
int d=f;
for(int v=t;v!=s;v=prevv[v]){
d=min(d,G[prevv[v]][preve[v]].cap);
}f-=d;
res+=d*h[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}return res;
}
const int MAX_N=50;
const int MAX_M=50;
int T,N,M;
int z[MAX_N][MAX_M];
void solve(){
int s=N+N*M,t=s+1;
V=t+1;
for(int i=0;i<=V;i++)G[i].clear();
for(int i=0;i<N;i++)add_edge(s,i,1,0);
for(int j=0;j<M;j++){
for(int k=0;k<N;k++){
add_edge(N+j*N+k,t,1,0);
for(int i=0;i<N;i++)add_edge(i,N+j*N+k,1,(k+1)*z[i][j]);
}
}printf("%.6f\n",(double)min_cost_flow(s,t,N)/N);
}
void init(){
scanf("%d%d",&N,&M);
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
scanf("%d",&z[i][j]);
}
}
}
int main(){
scanf("%d",&T);
while(T--){
init();
solve();
}return 0;
}
POJ 3686 The Windy's (费用流)的更多相关文章
- POJ 3686 The Windy's(思维+费用流好题)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5362 Accepted: 2249 Descr ...
- POJ 2195 Going Home(费用流)
http://poj.org/problem?id=2195 题意: 在一个网格地图上,有n个小人和n栋房子.在每个时间单位内,每个小人可以往水平方向或垂直方向上移动一步,走到相邻的方格中.对每个小人 ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- poj - 3686 The Windy's (KM算法)
题意:n个订单和m个生产车间,每个订单在不同的车间生产所需要的时间不一样,并且每个订单只能在同一个车间中完成,直到这个车间完成这个订单就可以生产下一个订单.现在需要求完成n个订单的平均时间最少是多少. ...
- POJ 3686 The Windy's 最小费用最大流
每个工厂拆成N个工厂,费用分别为1~N倍原费用. //#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...
- POJ 2195 D - Going Home 费用流
D - Going HomeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vie ...
- poj 3686 The Windy's
http://poj.org/problem?id=3686 #include <cstdio> #include <cstring> #include <algorit ...
- [ACM] POJ 3686 The Windy's (二分图最小权匹配,KM算法,特殊建图)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4158 Accepted: 1777 Descr ...
- POJ 3686 The Windy's (最小费用流或最佳完全匹配)
题意:有n个订单m个车间,每个车间均可以单独完成任何一个订单.每个车间完成不同订单的时间是不同的.不会出现两个车间完成同一个订单的情况.给出每个订单在某个车间完成所用的时间.问订单完成的平均时间是多少 ...
随机推荐
- 孤荷凌寒自学python第四十九天继续研究跨不同类型数据库的通用数据表操作函数
孤荷凌寒自学python第四十九天继续研究跨不同类型数据库的通用数据表操作函数 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 不同类型 ...
- Windows添加自定义服务、批处理文件开机自启动方法
[Windows 添加自定义服务方法]: 1.使用Windows服务工具instsrv.exe与srvany.exe: 参考:https://wenku.baidu.com/view/44a6e6f8 ...
- c语言版贪吃蛇小游戏
编译环境:windows 7 64位 编译工具:codeblocks 13.12 备注:未使用graphics.h 声明:个人原创,未经允许,禁止转载!!! 数据结构:双向链表 1.程序未使用grap ...
- 团队项目-第四次Scrum 会议
时间:10.26 时长:30分钟 地点:线上 工作情况 团队成员 已完成任务 待完成任务 解小锐 编写project和projectGenerator类 编写下一步的规格说明 陈鑫 采用creator ...
- SqlHelper——数据库小助手
SqlHelper其实就是一个类. 早就听说过"SqlHelper"这个名词,也查过相关的资料,但还是一头雾水.当真的去实践去用它时,就会发现其实它没那么神秘. 当敲第一个窗体的时 ...
- 写把proto函数搞清楚
在做blk层之前,先把proto搞清楚 ffi_lua metatype可以给函数加方法, lua中冒号是啥意思?冒号会传入self,但是点号不会传入self
- P1494 [国家集训队]小Z的袜子/莫队学习笔记(误
P1494 [国家集训队]小Z的袜子 题目描述 作为一个生活散漫的人,小\(Z\)每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小\(Z\)再也无法忍受这恼人的找袜子过程,于是他 ...
- ARM嵌入式开发中的GCC内联汇编__asm__
在针对ARM体系结构的编程中,一般很难直接使用C语言产生操作协处理器的相关代码,因此使用汇编语言来实现就成为了唯一的选择.但如果完全通过汇编代码实现,又会过于复杂.难以调试.因此,C语言内嵌汇编的方式 ...
- JS 问题整理 (1)
监听frame加载完毕 兼容Firefox/Opera/Safari/IE的处理方式,原文链接 var oFrm = document.getElementById('ifrm'); oFrm.onl ...
- pip切换国内的源
pip install Flask -i http://pypi.douban.com/simple ~/.pip/pip.conf The documentation says the config ...