【Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E】 【动态规划+网络流的思想】 Goods transportation

传送门:E. Goods transportation

描述:

E. Goods transportation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ito 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.

Input

The first line of the input contains two integers n and c (1 ≤ n ≤ 10 0000 ≤ 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.

Output

Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations.

Examples
input
3 0
1 2 3
3 2 1
output
4
input
5 1
7 4 2 1 0
1 2 3 4 5
output
12
input
4 3
13 10 7 4
4 7 10 13
output
34

题意:

给你n个城市,每个城市都可以往编号比自己大的城市运送c容量为物品

每个城市可以生产最多p[i]物品,最多售卖s[i]物品

然后问你这n个城市,最多总共能卖最多多少物品。

思路:

如果数据范围小的话,那么显然是网络流,建图的话s到n个城市为减容量为p[i]的边,然后n个城市到t建容量为s[i]的边,小的编号到大的编号建容量为c的边。

但是这个过不了。

考虑最大流等于最小割,我们可以考虑dp[i][j]表示考虑i个点,我们割掉了j个汇点的最小花费。

然后转移方程就是dp[i][j]=min(dp[i-1][j-1]+s[i],dp[i-1][j]+p[i]+cj)

然后滚动数组优化一下就好了

代码:

#include <bits/stdc++.h>
#define ll __int64
using  namespace  std;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}
const int N=1e4+10;
const ll inf=0x3f3f3f3f3f3f3f3f;

int n;
ll c,p[N],s[N],dp[2][N];

int  main(){
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif

  read(n);read(c);
  for(int i=1; i<=n; i++)read(p[i]);
  for(int i=1; i<=n; i++)read(s[i]);
  for(int i=1; i<=n; i++){
    dp[i&1][0]=dp[(i-1)&1][0]+p[i];
    for(int j=1; j<i; j++)
      dp[i&1][j]=min(dp[(i-1)&1][j]+j*c+p[i], dp[(i-1)&1][j-1]+s[i]);
    dp[i&1][i]=dp[(i-1)&1][i-1]+s[i];
  }
  ll ans=inf;
  for(int i=0; i<=n; i++)ans=min(ans, dp[n&1][i]);
  print(ans);
  return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值