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个车间,每个车间均可以单独完成任何一个订单.每个车间完成不同订单的时间是不同的.不会出现两个车间完成同一个订单的情况.给出每个订单在某个车间完成所用的时间.问订单完成的平均时间是多少 ...
随机推荐
- ASP.net MVC入门及Razor语法
一.MVC入门: 1.MVC简介 约定大于配置 2.MVC访问流程 csthml模板(razor模板)就是简化HTML的拼接的模板,最终还是生成html给浏览器显示,不能直接访问cshtml文件. 二 ...
- 2 28TOP100
import json import requests from requests.exceptions import RequestException import re import time d ...
- hexo从零开始到搭建完整 转
http://visugar.com/2017/05/04/20170504SetUpHexoBlog/ https://liuchi.coding.me/ look me 交流群 有相关问题的可 ...
- Mysql入门的10条语句
1.查看当前服务器下面,有那些库(database)? 答: show databases; 2.如何创建一个数据库? 答 : create database 最后面在写数据库名:分号结束 3.选择数 ...
- SystemTap 用法
SystemTap需要内核符号表: http://ddebs.ubuntu.com/pool/main/l/linux/ 基本语法: next对应C中的return,中途返回: 今晚遗留了两个问题: ...
- [hdu6432]Problem G. Cyclic
题目大意:给你$n$,一种合法的排列为,排列中没有$s[i\%n+1]-s[i]==1$,求合法方案数 题解:容斥,令$f_{i,j}$表示有$i$个元素,至少包含$j$个$s[i\%n+1]-s[i ...
- [NOWCODER] myh的超级多项式
题面 已知$f_i=(\sum_{j=1}^ka_j{v_j}^i )\bmod 1004535809$ 给定$v_1,v_2,\ldots,v_k,f_1,f_2,\ldots f_k$ 求$f_n ...
- Sublime Text 2 HTML代码缩进 美化HTML代码
关于代码格式的美化,之前在win下一直用“alignment”这个插件,它能实现一键对齐和缩进.最近使用mac版的sublime text 2,不知道是什么原因,这个插件疑似失效…… 这对有洁癖的完美 ...
- spring boot 2.0之后默认的连接池 HIkariCP介绍
HIkariCP链接池比之传统的Tomcat JDBC datasource .c3p0 datasource 等传统链接池优势太大,在获取链接释放链接,执行效率上面高出很多,这个产品的口号是“快速. ...
- Codevs 2080 特殊的质数肋骨
题目描述 Description 农民约翰的母牛总是产生最好的肋骨. 你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋 ...