Candy Factory

Time Limit: 2000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 4780
64-bit integer IO format: %I64d      Java class name: Main

  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

 

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的更多相关文章

  1. HDU 4780 Candy Factory(拆点费用流)

    Problem Description   A new candy factory opens in pku-town. The factory import M machines to produc ...

  2. Hdu 4465 Candy (快速排列组合+概率)

    题目链接: Hdu 4465 Candy 题目描述: 有两个箱子,每个箱子有n颗糖果,抽中第一个箱子的概率为p,抽中另一个箱子的概率为1-p.每次选择一个箱子,有糖果就拿走一颗,没有就换另外一个箱子. ...

  3. HDU 5536 Chip Factory 字典树

    Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  4. hdu 5536 Chip Factory (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...

  5. HDU 5291 Candy Distribution DP 差分 前缀和优化

    Candy Distribution 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5291 Description WY has n kind of ...

  6. HDU 5536 Chip Factory 【01字典树删除】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536 Chip Factory Time Limit: 18000/9000 MS (Java/Ot ...

  7. 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  8. HDU 5536 Chip Factory 字典树+贪心

    给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...

  9. hdu 1034 Candy Sharing Game

    Candy Sharing Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. uml图六种箭头的含义(转载)

    在看一些技术博客的时候,经常会见到博客里画上很多uml图.因为经常会被这几种表达关系的箭头搞混,这里我就把常见的6种箭头表达的含义理一下. 泛化 概念:泛化是一种一般与特殊.一般与具体之间关系的描述, ...

  2. [转载]android常用的API接口调用

    原文地址:android常用的API接口调用作者:宋耀 显示网页:         Uri uri = Uri.parse("http://www.google.com"); In ...

  3. bzoj 4809: 皇后【dfs】

    爆搜卡线过 并不知道正解是啥 #include<iostream> #include<cstdio> using namespace std; const int N=40; ...

  4. post和get区别,其他答案真的太坑

    原理: get和post都是http定义与服务器交互的方法,还有put,delete url是网络上的资源,那么http中的get,post,put,delete对应的就是对这个资源的查,改,增,删四 ...

  5. [POI2013]POL-Polarization

    题目描述 Everyone knew it would only be a matter of time. So what? Faced for years on, a peril becomes t ...

  6. linux学习之路7 linux下获取帮助

    help 帮助 ls -h或者ls - -help man 最常用的帮助命令 man (+数字 )+命令 (数字代表文档帮助类型) man -k 关键字 可以用来查询包含该关键字的文档 info 与m ...

  7. 设置VMWare虚拟机使拷贝虚拟机后固定原有的IP地址

    VMWare中已经安装并设置好的虚拟机在拷贝给别人后,再次打开该虚拟机时原有自动获取的IP地址将会变化,那么原有根据该IP地址进行的设置均将失效,还需要重新设置,比较麻烦,经过百度查询原来可以将虚拟机 ...

  8. sql server用SQL语句查看字段说明

    SELECT t.[name] AS 表名,c.[name] AS 字段名,cast(ep.[value] )) AS [字段说明] FROM sys.tables AS t INNER JOIN s ...

  9. [Windows Server 2012] 更换PHP版本方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:更换PHP ...

  10. C++标准库 vector排序

    前天要做一个对C++ STL的vector容器做一个排序操作,之前一直把vector当做一个容量可自动变化的数组,是的,数组,所以打算按照对数组进行排序的方法:用快速排序或是冒泡排序等算法自己写一个排 ...