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
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 ...
随机推荐
- golang——strconv包常用函数
1.func ParseBool(str string) (value bool, err error) 返回字符串表示的bool值.它接受1.0.t.f.T.F.true.false.True.Fa ...
- ROS-URDF仿真
前言:URDF (标准化机器人描述格式),是一种用于描述机器人及其部分结构.关节.自由度等的XML格式文件. 一.首先做一个带有四个轮子的机器人底座. 1.1 新建urdf文件 在chapter4_t ...
- [C++ STL] set使用详解
一.set介绍: set容器内的元素会被自动排序,set与map不同,set中的元素即是键值又是实值,set不允许两个元素有相同的键值.不能通过set的迭代器去修改set元素,原因是修改元素会破坏se ...
- 多维DP UVA 11552 Fewest Flop
题目传送门 /* 题意:将子符串分成k组,每组的字符顺序任意,问改变后的字符串最少有多少块 三维DP:可以知道,每一组的最少块是确定的,问题就在于组与组之间可能会合并块,总块数会-1. dp[i][j ...
- ACM_18(同余)
18 Time Limit: 2000/1000ms (Java/Others) Problem Description: 一个简单的问题,大家应该有不少做过大数加法题吧.这个题的数据也是64位整数放 ...
- Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解
步骤1.破解包下载地址:https://gitee.com/pengzhile/MyBatisCodeHelper-Pro-Crack/releases 步骤2.下载:Intellij IDEA p ...
- Spark学习之键值对(pair RDD)操作(3)
Spark学习之键值对(pair RDD)操作(3) 1. 我们通常从一个RDD中提取某些字段(如代表事件时间.用户ID或者其他标识符的字段),并使用这些字段为pair RDD操作中的键. 2. 创建 ...
- Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载
元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...
- Android PopupWindow使用时注意
项目中使用PopupWindown出现的坑 1.部分设备,PopWindow在Android4.0后版本,出现NullPointerException调用以下方法可解决, fixPopupWindow ...
- Spartan6系列之器件详细介绍、选型参考
1. 概述 Spartan6系列是一类低成本高容量的FPGA,采用45nm低功耗敷铜技术,能在功耗.性能.成本之间很好地平衡:Spartan6系列内部采用双寄存器.6输入的LUT,还有一系列 ...