Goods transportation

Description

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 p i   units of goods. No more than s i   units of goods can be sold in the i  -th city.

For each pair of cities i  and j  such that 1i<jn  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.

Input

The first line of the input contains two integers n  and c  (1n10000 , 0c10 9   ) — the number of cities and the maximum amount of goods for a single transportation.

The second line contains n  integers p i   ( 0p i 10 9   ) — the number of units of goods that were produced in each city.

The third line of input contains n integers s i   ( 0s i 10 9   ) — 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.

Solution

Build a network of the following form: The network will consist of n+2  vertices, the source located at the node with the number n+1  and the sink at the node with the number n+2  . For each node i  such that 1in  add the arc (n+1,i)  with a capacity p i   and arc (i,n+2)  with capacity s i   . Also, for each pair of nodes i  and j  such that 1i<jn  , add the arc (i,j)  with capacity m.

The value of the maximum flow in this network is the answer to the problem. Since the network contains O(n 2 )  arcs, the usual flow search algorithms is not applicable here.

Instead, we will find a capacity of the minimal cut in this network. The cut in this case is a partition of the set of all nodes from 1  to n+2  into two parts. Let A  be the part that contains the source and 这里写图片描述 — all other nodes. Then the capacity of the cut is equal to 这里写图片描述.

Capacity of the minimal cut can be found using the following dynamic programming: cut(i,j)  will be the minimum capacity of the cut, which was built on the first i  nodes, such that the set A  contains exactly j  nodes other than the source. It is easy to get the formula for this dynamic: cut(i,j)=min(cut(i1,j1)+s i ,cut(i1,j)+p i +jc) .

The answer to the problem can be obtained as min j=0,n cut(n,j)  .

The resulting complexity of this solution is O(n 2 )  . To fit in memory limit you should store only two consecutive rows during the computation of the dynamic.

翻译一下就是考虑网络流:
1. S  to i  : p i  
2. i  to T  : s i  
3. i  to j  : c  (i<j) 
最大流就是答案,但直接跑是 O(n 2 )  级别的边,肯定爆炸。考虑到这个网络的特殊性,同时考虑到最大流等于最小割。设计状态 f[i][j]  表示对于前 i  个点中有 j  个点分到了 S  集合的最小割边代价,转移为 f[i][j]=min(f[i1][j1]+s i ,f[i1][j]+p i +jc) ,转移时间复杂度 O(n 2 )  ,空间上需要滚动一下。

Source

http://codeforces.com/contest/724/problem/E

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值