[codeforces724E]Goods transportation
[codeforces724E]Goods transportation
试题描述
There are n cities located along the one-way road. Cities are numbered from 1 to n in the direction of the road.
The i-th city had produced pi units of goods. No more than si units of goods can be sold in the i-th city.
For each pair of cities i and j such that 1 ≤ i < j ≤ n you can no more than once transport no more than c units of goods from the city i to the city j. Note that goods can only be transported from a city with a lesser index to the city with a larger index. You can transport goods between cities in any order.
Determine the maximum number of produced goods that can be sold in total in all the cities after a sequence of transportations.
输入
The first line of the input contains two integers n and c (1 ≤ n ≤ 10 000, 0 ≤ c ≤ 109) — the number of cities and the maximum amount of goods for a single transportation.
The second line contains n integers pi (0 ≤ pi ≤ 109) — the number of units of goods that were produced in each city.
The third line of input contains n integers si (0 ≤ si ≤ 109) — the number of units of goods that can be sold in each city.
输出
Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations.
输入示例
输出示例
数据规模及约定
见“输入”
题解
这显然是一个最大流模型,从源点向每一个城市连一条容量为 pi 的单向边,从每个城市向汇点连一条容量为 si 的单向边,然后每个城市向编号比它大的所有城市连一条容量为 c 的单向边。
然而最多有 10002 个点,50015000 条边,时间暂且不考虑,每条边需要有反向弧,并且每条弧至少需要存 2 个 int(边的终点和边的剩余流量),对于 256MB 的限制来说太大了。
然而这题有一个比较妙的办法,由于建图的特殊性,我们可以转化成最小割之后 dp,设 f(i, j) 表示前 i 个点中 j 个点属于 S 集合的最小割是多少。
第一步:先把从源点到节点 i 再到汇点的流量先耗尽,于是残量网络就变成了这个样子:
若 pi > si,则从源点向 i 连一条 pi - si 的边;
若 pi < si,则从 i 向汇点连一条 si - pi 的边;
若 0 < i < j < n + 1,则从 i 向 j 连一条 c 的边。
第二步:考虑上面的 dp 如何转移,枚举当前点属于 S 集还是 T 集。我很懒,不想再写了,转移方程不妨留给读者思考吧。。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 10010
#define oo (1ll << 60)
#define LL long long
LL f[2][maxn];
int n, C, P[maxn], S[maxn]; int main() {
n = read(); C = read();
for(int i = 1; i <= n; i++) P[i] = read();
for(int i = 1; i <= n; i++) S[i] = read(); for(int i = 0; i < 2; i++)
for(int j = 0; j <= n; j++)
f[i][j] = oo;
f[0][0] = 0;
LL base = 0; bool cur = 1;
for(int i = 1; i <= n; i++, cur ^= 1) {
base += min(S[i], P[i]);
for(int j = 0; j <= n; j++) f[cur][j] = oo;
for(int j = 0; j <= i; j++) {
if(S[i] > P[i]) {
if(j && f[cur^1][j-1] < oo) f[cur][j] = min(f[cur][j], f[cur^1][j-1] + S[i] - P[i]);
if(f[cur^1][j] < oo) f[cur][j] = min(f[cur][j], f[cur^1][j] + (LL)j * C);
}
else {
if(j && f[cur^1][j-1] < oo) f[cur][j] = min(f[cur][j], f[cur^1][j-1]);
if(f[cur^1][j] < oo) f[cur][j] = min(f[cur][j], f[cur^1][j] + P[i] - S[i] + (LL)j * C);
}
}
}
LL tmp = oo;
for(int i = 0; i <= n; i++) tmp = min(tmp, f[cur^1][i]); printf("%I64d\n", base + tmp); return 0;
}
[codeforces724E]Goods transportation的更多相关文章
- Goods transportation
Goods transportation time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation 动态规划
E. Goods transportation 题目连接: http://codeforces.com/contest/724/problem/E Description There are n ci ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E - Goods transportation 最大流转最小割转dp
E - Goods transportation 思路:这个最大流-> 最小割->dp好巧妙哦. #include<bits/stdc++.h> #define LL long ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)
题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...
- Codeforces 724 E Goods transportation
Description 有 \(n\) 个点,每个点有一个入流和出流,每个点与编号比它大的点连边,容量为 \(c\) ,求最大流. Sol DP. 这种特殊的图可以DP,把最大流转化成最小割. 最小割 ...
- CF724E Goods transportation 最小割 DP
照惯例CF的题不放原题链接... 题意:一个序列上有n个点,每个点有权值pi和si.表示这个点一开始有pi个物品,最多可以卖出si个物品,每个点都可以把物品向编号更大的点运输,但是对于i < j ...
- CF724E Goods transportation
最大流既视感 然后 TLEMLE既视感 然后 最大流=最小割 然后 dp[i][j]前i个点j个点在S集合,最小割 然后 dp[i][j]=min(dp[i-1][j]+p[i]+j*c,dp[i-1 ...
- Codeforces 724E Goods transportation(最小割转DP)
[题目链接] http://codeforces.com/problemset/problem/724/E [题目大意] 每个城市有pi的物品可以运出去卖,si个物品可以买, 编号小的城市可以往编号大 ...
- CodeForces E. Goods transportation【最大流+dp最小割】
妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...
随机推荐
- pip 安装管理失败解决
问题: pip install ansible 错误: src/hash_template.c:361: warning: implicit declaration of function ‘Py_F ...
- 批量部署Hadoop集群环境(1)
批量部署Hadoop集群环境(1) 1. 项目简介: 前言:云火的一塌糊涂,加上自大二就跟随一位教授做大数据项目,所以很早就产生了兴趣,随着知识的积累,虚拟机已经不能满足了,这次在服务器上以生产环境来 ...
- Java 11 正式发布,支持期限至2026年9月
美国当地时间9月25日,Oracle 官方宣布 Java 11 (18.9 LTS) 正式发布,可在生产环境中使用!这是自 Java 8 后的首个长期支持版本,非常值得大家的关注,可以通过下面的地址进 ...
- The lion king 经典句型摘录
What am I going to do with him? Everything the light touches is our kingdom. But I thought a king ca ...
- iOS猜拳游戏源码
利用核心动画和Quartz2D做的一个小游戏.逻辑十分简单. 源码下载:http://code.662p.com/<ignore_js_op> 详细说明:http://ios.662p.c ...
- iOS开发内购全套图文教程
2015年最全的内购图文教程,首先是填各种资料,最后是代码,废话不多说,直接上图 ======================第一部分协议=============== 第一步 第二步 第三步 第四步 ...
- SQLite -创建表
SQLite -创建表 SQLite CREATE TABLE语句用于创建一个新表在任何给定的数据库.创建一个基本表包括表命名和定义其列,每列的数据类型 语法: CREATE TABLE语句的基本语法 ...
- saltstack 源码安装
面向对象编程(oop) 面向对象: 面向对象三大特性: 封装 继承 多肽封装: 封装就是将具体的客观事物封装成抽象的类.并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可行的进行信息隐藏继承 ...
- 进程池_Pool
当需要创建子进程数量不多的时候,可以直接利用multiprocessing中的Process动态生成多个进程 但是如果是成百甚至上千个任务,手动地创建它的工作量很大,此时就可以利用到multiproc ...
- gitee 如何创建仓库 及发布
gitee 如何创建仓库 及发布 http://pengchenggang.gitee.io/layuisyshelp ----------- 在本地项目文件中使用bash $ git config ...