Problem Description
  A new candy factory opens in pku-town. The factory import M machines to produce high quality candies. These machines are numbered from 1 to M.
  There are N candies need to be produced. These candies are also numbered from 1 to N. For each candy i , it can be produced in any machine j. It also has a producing time(si,ti) , meaning that candy i must start producing at time si and will finish at ti. Otherwise if the start time is pi(si < pi < ti) then candy will still finish at ti but need additional K*(pi - si) cost. The candy can’t be produced if pi is greater than or equal to ti. Of course one machine can only produce at most one candy at a time and can’t stop once start producing.
  On the other hand, at time 0 all the machines are in their initial state and need to be “set up” or changed before starting producing. To set up Machine j from its initial state to the state which is suitable for producing candiy i, the time required is Cij and cost is Dij. To change a machine from the state suitable for candy i1 into the state suitable for candy i2, time required is Ei1i2 and cost is Fi1i2.
  As the manager of the factory you have to make a plan to produce all the N candies. While the sum of producing cost should be minimized.
 
Input
  There are multiple test cases.
  For each case, the first line contains three integers N(1<=N<=100), M(1<=M<=100), K(1<=K<=100) . The meaning is described above.
  Then N lines follow, each line contains 2 integers si and ti(0 <= si < ti <100000).
  Then N lines follow, each line contains M integers, the j-th integer of the i-th line indicating Cij(1<=Cij<=100000) .
  Then N lines follow, each line contains M integers, the j-th integer of the i-th line indicating Dij(1<=Dij<=100000) .
  Then N lines follow, each line contains N integers, the i2-th integer of the i1-th line indicating Ei1i2(1<=Ei1j2<=100000) . 
  Then N lines follow, each line contains N integers, the i2-th integer of the i1-th line indicating Fi1i2(1 <= Fi1j2<=100000) . 
  Since the same candy will only be produced once, Eii and Fii are meaningless and will always be -1.
  The input ends by N=0 M=0 K=0. Cases are separated with a blank line.
 
Output
For each test case, if all of M candies can be produced, output the sum of minimum producing cost in a single line. Otherwise output -1.
 
Sample Input
3 2 1
4 7
2 4
8 9
4 4
3 3
3 3
2 8
12 3
14 6
-1 1 1
1 -1 1
1 1 -1
-1 5 5
5 -1 5
5 5 -1

1 1 2
1 5
5
5
-1
-1

0 0 0

 
Sample Output
11
-1

Hint

For the first example, the answer can be achieved in the following way:
In the picture, S i represents setting up time for candy i, A i represents changing time for candy i and P i represents producing time for candy i .
So the total cost includes: 

setting up machine 1 for candy 1, costs 2
setting up machine 2 for candy 2, costs 3
changing state from candy 2 to candy 3, costs 5
late start of candy 2, costs 1
题意
有N个糖果需要被生产,有M个机器,K代表额外花费系数
N行代表第i个糖果只能在[s[i],t[i]]的时间内被生产
接下来N*M代表生产第i个糖果的机器j在C[i][j]时间被开启
接下来N*M代表生产糖果i机器j的基础花费D[i][j],如果在第s[i]<p<t[i]的时间开始生产,也可以在t[i]生产出糖果,但需要额外花费(p-s[i])*K的花费
接下来N*N代表把机器J正在生产i糖果改成生产j糖果,所需的额外时间为E[i][j]
接下来N*N代表把机器J正在生产i糖果改成生产j糖果,所需的基础花费F[i][j],如果在第s[j]<p<t[j]的时间开始生产,也可以在t[j]生产出糖果,但需要额外花费(p-s[j])*K的花费
题解
一道比较复杂的费用流题,题意读了很久
为了保证糖果从源点S流出一次从汇点T流入一次,可以把N个糖果拆成左右糖,然后(S,左糖,1,0),然后(右糖,T,1,0)
然后考虑一个机器生产完i再去生产j的情况,如果时间允许,就连(左糖i,右糖j,1,基础花费F+额外花费)
然后新建一个点u,(S,u,m,0),(u,每个机器,1,0),如果机器i生产糖果j的时间C[j][i]<t[j]就连线(机器,右糖,1,基础花费D+额外花费)
代码
 #include<bits/stdc++.h>
using namespace std; const int N=1e5+;
const int M=2e5+;
const int INF=0x3f3f3f3f; int FIR[N],FROM[M],TO[M],CAP[M],FLOW[M],COST[M],NEXT[M],tote;
int pre[N],dist[N],q[];
bool vis[N];
int n,m,S,T;
void init()
{
tote=;
memset(FIR,-,sizeof(FIR));
}
void addEdge(int u,int v,int cap,int cost)
{
FROM[tote]=u;
TO[tote]=v;
CAP[tote]=cap;
FLOW[tote]=;
COST[tote]=cost;
NEXT[tote]=FIR[u];
FIR[u]=tote++; FROM[tote]=v;
TO[tote]=u;
CAP[tote]=;
FLOW[tote]=;
COST[tote]=-cost;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
bool SPFA(int s, int t)
{
memset(dist,INF,sizeof(dist));
memset(vis,false,sizeof(vis));
memset(pre,-,sizeof(pre));
dist[s] = ;vis[s]=true;q[]=s;
int head=,tail=;
while(head!=tail)
{
int u=q[++head];vis[u]=false;
for(int v=FIR[u];v!=-;v=NEXT[v])
{
if(dist[TO[v]]>dist[u]+COST[v]&&CAP[v]>FLOW[v])
{
dist[TO[v]]=dist[u]+COST[v];
pre[TO[v]]=v;
if(!vis[TO[v]])
{
vis[TO[v]] = true;
q[++tail]=TO[v];
}
}
}
}
return pre[t]!=-;
}
void MCMF(int s, int t, int &cost, int &flow)
{
flow=;
cost=;
while(SPFA(s,t))
{
int Min=INF;
for(int v=pre[t];v!=-;v=pre[TO[v^]])
Min=min(Min,CAP[v]-FLOW[v]);
for(int v=pre[t];v!=-;v=pre[TO[v^]])
{
FLOW[v]+=Min;
FLOW[v^]-=Min;
cost+=COST[v]*Min;
}
flow+=Min;
}
}
int main()
{
int N,M,K,u,ss[],tt[],C[][],D[][],E[][],F[][];
while(scanf("%d%d%d",&N,&M,&K)!=EOF,N||M||K)
{
init();
for(int i=;i<=N;i++)
scanf("%d%d",&ss[i],&tt[i]);
for(int i=;i<=N;i++)for(int j=;j<=M;j++)
scanf("%d",&C[i][j]);
for(int i=;i<=N;i++)for(int j=;j<=M;j++)
scanf("%d",&D[i][j]);
for(int i=;i<=N;i++)for(int j=;j<=N;j++)
scanf("%d",&E[i][j]);
for(int i=;i<=N;i++)for(int j=;j<=N;j++)
scanf("%d",&F[i][j]); S=,u=N+N+M+,T=N+N++M+,n=T;
for(int i=;i<=N;i++)
{
addEdge(S,i,,);
addEdge(N+i,T,,);
for(int j=;j<=N;j++)
{
if(i!=j&&tt[i]+E[i][j]<tt[j])
{
int c=(max(tt[i]+E[i][j],ss[j])-ss[j])*K;
addEdge(i,N+j,,c+F[i][j]);
}
}
}
addEdge(S,u,M,);
for(int i=;i<=M;i++)
{
addEdge(u,N+N+i,,);
for(int j=;j<=N;j++)
{
if(C[j][i]<tt[j])
{
int c=(max(C[j][i],ss[j])-ss[j])*K;
addEdge(N+N+i,N+j,,c+D[j][i]);
}
}
}
int flow,cost;
MCMF(S,T,cost,flow);
if(flow!=N)printf("-1\n");
else printf("%d\n",cost);
}
return ;
}

HDU 4780 Candy Factory(拆点费用流)的更多相关文章

  1. HDU 4780 Candy Factory

    Candy Factory Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

  2. BZOJ 1877 晨跑 拆点费用流

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1877 题目大意: Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧 ...

  3. CF 277E Binary Tree on Plane (拆点 + 费用流) (KM也可做)

    题目大意: 平面上有n个点,两两不同.现在给出二叉树的定义,要求树边一定是从上指向下,即从y坐标大的点指向小的点,并且每个结点至多有两个儿子.现在让你求给出的这些点是否能构成一棵二叉树,如果能,使二叉 ...

  4. HDU 5988 Coding Contest(浮点数费用流)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...

  5. HDU 4744 Starloop System(ZKW费用流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4744 题意:三维空间n个点,每个点有一个wi值.每对点的距离定义为floor(欧拉距离),每对点之间建 ...

  6. HDU 5644 King's Pliot【费用流】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 题意: 每天都有p[i]个飞行员进行阅兵,飞行员只工作一天. m个休假公式,花费tt[i]元让 ...

  7. 【拆点费用流】【HDU1853】【 Cyclic Tour】

    题意: 有N个城市,M条单向路,Tom想环游全部城市,每次至少环游2个城市,每个城市只能被环游一次.由于每条单向路都有长度,要求游遍全部城市的最小长度. // 给定一个有向图,必须用若干个环来覆盖整个 ...

  8. 洛谷P2604 网络扩容 拆点+费用流

    原题链接 这题貌似比较水吧,最简单的拆点,直接上代码了. #include <bits/stdc++.h> using namespace std; #define N 1000 #def ...

  9. HDU - 2732 Leapin' Lizards (拆点最大流)

    题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...

随机推荐

  1. ScriptManager 发送错误到客户端

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  2. ubuntu deb pacakge 开发

    安装构建工具 apt-get install pbuilder 推荐安装 sudo apt-get install build-essential autoconf automake \ autoto ...

  3. mysql悲观锁

    悲观锁与乐观锁是两种常见的资源并发锁设计思路,也是并发编程中一个非常基础的概念. 悲观锁(Pessimistic Lock) 悲观锁的特点是先获取锁,再进行业务操作,即“悲观”的认为获取锁是非常有可能 ...

  4. pycharm 3.4 破解

    修改host,增加一行: 0.0.0.0 account.jetbrains.com 使用Activate code注册: EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUl ...

  5. bzoj5047: 空间传送装置

    Description 太空中一共有n座星球,它们之间可以通过空间传送装置进行转移.空间传送装置分为m种,第i种装置可以用4个参 数a_i,b_i,c_i,d_i来描述.因为时空抖动的问题,在非整数时 ...

  6. 洛谷P1443马的遍历

    传送 这是个广搜,思路和普通的迷宫题差不多,但我卡了3遍,为什么呢? 因为输出格式 题目要求左对齐,宽度为5输出,在此说一下如何控制宽度. 下面的m都为要求的宽度 int 类型: printf: %m ...

  7. unittest 出报告 并配合 jenkins,发现有用例错误,但是构建没出现红点 的解决方法

    加了个 判断 测试用例总数 和 测试运行成功数 是否一致的判断,不一致 就断言失败,jenkins哪里是红点

  8. spring mvc 注解整理(一)

    @Controller和@RestController: RestController = @ResponseBody + @Controller  所有返回都是json类型,无法跳转到jsp页面,但 ...

  9. java中如何给控件设置颜色

     1. tv.setTextColor(Color.parseColor("#000000"));2. tv.setTextColor(getResources().getCo ...

  10. DllImport使用

    1.Dll引用路径 (1)exe运行程序所在的目录 (2)System32目录 (3)环境变量目录 (4)自定义路径,如:DllImport(@"C:\OJ\Bin\Judge.dll&qu ...