【题目链接】 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 (费用流)的更多相关文章

  1. POJ 3686 The Windy's(思维+费用流好题)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5362   Accepted: 2249 Descr ...

  2. POJ 2195 Going Home(费用流)

    http://poj.org/problem?id=2195 题意: 在一个网格地图上,有n个小人和n栋房子.在每个时间单位内,每个小人可以往水平方向或垂直方向上移动一步,走到相邻的方格中.对每个小人 ...

  3. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  4. poj - 3686 The Windy's (KM算法)

    题意:n个订单和m个生产车间,每个订单在不同的车间生产所需要的时间不一样,并且每个订单只能在同一个车间中完成,直到这个车间完成这个订单就可以生产下一个订单.现在需要求完成n个订单的平均时间最少是多少. ...

  5. POJ 3686 The Windy's 最小费用最大流

    每个工厂拆成N个工厂,费用分别为1~N倍原费用. //#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...

  6. POJ 2195 D - Going Home 费用流

    D - Going HomeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vie ...

  7. poj 3686 The Windy's

    http://poj.org/problem?id=3686 #include <cstdio> #include <cstring> #include <algorit ...

  8. [ACM] POJ 3686 The Windy&#39;s (二分图最小权匹配,KM算法,特殊建图)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4158   Accepted: 1777 Descr ...

  9. POJ 3686 The Windy's (最小费用流或最佳完全匹配)

    题意:有n个订单m个车间,每个车间均可以单独完成任何一个订单.每个车间完成不同订单的时间是不同的.不会出现两个车间完成同一个订单的情况.给出每个订单在某个车间完成所用的时间.问订单完成的平均时间是多少 ...

随机推荐

  1. 重复造轮子系列--dijkstra算法

    前年一时脑热(理想很丰满,现实很骨感),写了这个最短路径优先的低效版本,且留着回忆吧. spf.h #ifndef SPF_H_ #define SPF_H_ typedef struct { int ...

  2. Android M中 JNI的入门学习

    今年谷歌推出了Android 6.0,作为安卓开发人员,对其学习掌握肯定是必不可少的,今天小编和大家分享的就是Android 6.0中的 JNI相关知识,这是在一个安卓教程网上看到的内容,感觉很不错, ...

  3. linux系统初始化——文件系统初始化步骤

    linux文件系统初始化步骤 System V init启动过程 概括地讲,Linux/Unix系统一般有两种不同的初始化启动方式. 1) BSD system init 2) System V in ...

  4. 《c程序设计语言》-3.2 字符串转换

    #include <stdio.h> #define Num 1000 int main() { int i = 0,j,k,count2 = 0; char s[Num] = {'\0' ...

  5. Windows Server 创建环回网卡

    1.以管理员身份运行cmd后,在cmd命令窗口中执行:hdwwiz 启动硬件添加向导. 2.在添加硬件向导中选择手动安装或自动搜索都可以.然后选择网络适配器. 3.选择网络适配器:厂商选择Micros ...

  6. Debian中文字体安装

    默认装的英文办的debian7,看中国字不太美,这好办照着做吧 1. Setup locales #dpkg-reconfigure locales 选择 zh_CN GB2312 zh_CN.GBK ...

  7. 关于try-catch-finally return 的面试题

    public class Test { public static void main(String[] args) { System.out.println(test()); } static in ...

  8. 【Web前端】把图片嵌入到css样式表中(附小工具)

    适用场景: 本地通过stylish等插件自定义网站样式时 开通css自定义的空间但暂无图片上传途径时 ……   举例:     把视频页的缩略图边框改为下面这种        .main_list u ...

  9. VLC for iOS 2.3.0

    http://www.cocoachina.com/bbs/read.php?tid=231898 VLC for iOS 2.3.0       本帖属于CocoaChina会员发表,转帖请写明来源 ...

  10. git的使用01

    直接下载安装git,这里就不演示了,如果安装成功,在桌面任意空白处单击鼠标右键,会多出两个选项 Git Gui Here和Git Bash Here,我们一般使用git bash here 右键之后点 ...