HDU 4780 Candy Factory
Candy Factory
This problem will be judged on HDU. Original ID: 4780
64-bit integer IO format: %I64d Java class name: Main
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
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
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
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
Source
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct arc {
int to,flow,cost,next;
arc(int x = ,int y = ,int z = ,int nxt = -) {
to = x;
flow = y;
cost = z;
next = nxt;
}
} e[maxn*maxn];
int head[maxn],p[maxn],d[maxn],S,T,tot,n,m,k;
void add(int u,int v,int flow,int cost) {
e[tot] = arc(v,flow,cost,head[u]);
head[u] = tot++;
e[tot] = arc(u,,-cost,head[v]);
head[v] = tot++;
}
queue<int>q;
bool in[maxn];
bool spfa() {
while(!q.empty()) q.pop();
memset(p,-,sizeof p);
memset(d,0x3f,sizeof d);
d[S] = ;
q.push(S);
while(!q.empty()) {
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] > d[u] + e[i].cost) {
d[e[i].to] = d[u] + e[i].cost;
p[e[i].to] = i;
if(!in[e[i].to]) {
in[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
return p[T] > -;
}
void solve() {
int ret = ,flow = ;
while(spfa()) {
int minF = INF;
for(int i = p[T]; ~i; i = p[e[i^].to])
minF = min(minF,e[i].flow);
for(int i = p[T]; ~i; i = p[e[i^].to]) {
e[i].flow -= minF;
e[i^].flow += minF;
}
ret += minF*d[T];
flow += minF;
}
printf("%d\n",flow < n?-:ret);
}
int ss[maxn],tt[maxn],C[maxn][maxn],D[maxn][maxn],E[maxn][maxn],F[maxn][maxn];
void Read() {
memset(head,-,sizeof head);
for(int i = tot = ; 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);
}
int main() {
while(scanf("%d%d%d",&n,&m,&k),n||m||k) {
Read();
S = *n + m;
T = S + ;
for(int i = ; i < n; ++i) {
add(S,i,,);
add(i + n,T,,);
}
for(int i = ; i < m; ++i)
add(n + n + i,T,,);
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j) {
if(C[i][j] >= tt[i]) continue;
int cost = D[i][j];
if(C[i][j] > ss[i]) cost += (C[i][j] - ss[i])*k;
add(i,*n + j,,cost);
}
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j) {
if(i == j) continue;
int ctime = tt[i] + E[i][j];
if(ctime >= tt[j]) continue;
int cost = F[i][j];
if(ctime > ss[j]) cost += k*(ctime - ss[j]);
add(j,i + n,,cost);
}
solve();
}
return ;
}
HDU 4780 Candy Factory的更多相关文章
- HDU 4780 Candy Factory(拆点费用流)
Problem Description A new candy factory opens in pku-town. The factory import M machines to produc ...
- Hdu 4465 Candy (快速排列组合+概率)
题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子. ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- hdu 5536 Chip Factory (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...
- HDU 5291 Candy Distribution DP 差分 前缀和优化
Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...
- HDU 5536 Chip Factory 【01字典树删除】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...
- 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- HDU 5536 Chip Factory 字典树+贪心
给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...
- hdu 1034 Candy Sharing Game
Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- bzoj 4031: [HEOI2015]小Z的房间【矩阵树定理】
是板子题,因为mod不是质数所以需要辗转相除然而并不知道为啥 高斯消元部分还不知道原理呢--先无脑背过的 #include<iostream> #include<cstdio> ...
- 清北考前刷题day7早安
- Java中的锁机制,你真的了解吗?
学到锁说明你已经学过多线程了,只有在多线程并发的情况下才会涉及到锁,相信大家用的最多的要数synchronized了,因为这个也是最简单的,直接加在方法上就可以使一个方法同步.那么除了synchron ...
- SQL数据库语言基础
表的创建: 1.创建列(字段):列名+类型 2.设置主键列:能够唯一标识一条数据 3.设置唯一:内容不能重复 4.外键关系: 一张表(从表)其中的某列引用自另外一张表(主表)中的主键列 设计表: 数据 ...
- java 装饰者类
装饰者模式:增强一个类的功能还可以让装饰者类之间互相装饰. 装饰者模式和继承的区别: 继承实现的增强类: 优点:代码结构清晰,而且实现简单 缺点:对于每一个的需要增强的类都要创建具体的子类来帮助其增强 ...
- LN : Eden Bitset_3
Appreciation to our TA, 王毅峰, who designed this task. 问题描述 Give you N numbers a[1]...a[n] and M numbe ...
- Scala-基础-数组(1)
import junit.framework.TestCase import scala.collection.mutable.ArrayBuffer; //数组(1) //知识点-定义数组,变长数组 ...
- Django--2、form表单
django中定义form表单的优势 HTML中提交后,若数据出现错误,返回的页面中仍然可以保留之前输入的数据. 通过校验规则可以方便的限制字段条件并校验. 在Django中建个form表单 先要确定 ...
- python--12、数据库进阶
SQL语句关键词: #再次不做过多介绍 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 使用SELECT查询数据以及. #示例中department为部门表 ...
- Python之绘图和可视化
Python之绘图和可视化 1. 启用matplotlib 最常用的Pylab模式的IPython(IPython --pylab) 2. matplotlib的图像都位于Figure对象中. 可以使 ...