[动态规划]Pku3377--Ferry Lanes

http://acm.pku.edu.cn/JudgeOnline/problem?id=3377

 

Ferry Lanes
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 2563 Accepted: 510

Description

Arthur lives in a small city which is partitioned into two districts, the northern and the southern, by a river flowing through. The northern and southern districts are connected by N + 1 bidirectional ferry lanes, numbered 0 to N from west to east. Each ferry lane connects two docks in separate sides of the river. No two lanes share the same dock or cross each other.

Today Arthur needs to deliver a package from one dock to another. He knows the sailing time of each ferry lane and the time cost by walking from one dock to an adjacent one along the river bank. Arthur wants to know what is the minimum time his delivery will cost.

 

 

Input

The input consists of several test cases. The first line of each consists an integer N ( 1 ≤ N ≤ 1,000,000). The second line consists of two pairs of integers describing the starting and finishing docks, where the first of each pair represents the district (0 means northern and 1 means southern) and the second represents the lane number. The third line contains N integers describing the time cost by walking between two adjacent docks on the northern bank from lane0 to laneN. The fourth line contains N + 1 integers describing the sailing time of each ferry lane. The last line contains N integers describing the time cost by walking between two adjacent docks on the southern bank from lane0 to laneN. N = 0 indicates the end of input.

Output

For each test cases output one line contains the minimum time. You may assume the answer fits in a signed 64-bit integer.

Sample Input

4
0 0 1 4
1 3 5 7
3 5 1 3 7
1 3 7 5
0

Sample Output

17

Source

 

题目大意:如题目的图中所示,南北岸各有n+1个船坞,编号为0~n,给出船坞之间的双向通路和水路(一个船坞对应一条水路,水路不交叉)所需时间,求从指点船坞到指点船坞最少耗时。

 

分析:可以用最短路解决,不过n有10^6之大,所以要用spfa之类的高效算法才行。不一定要构图,因为边是一定的。

我用的是O(n)的dp。

如果给出的起点坐标大于终点坐标,则交换。令f[i,0]f[i,1]分别表示走到北岸第i个点和南岸第i个点的最少时间,不难得到:

f[i,0]:=min(f[i-1,0]+cost,f[i-1,1]+cost+lane).

f[i,1]同理。

初始,要先求出从起点到对岸的最短路。只要向左扫描下就行了。

dp之后不能马上得到最小值,因为可以在对岸向右后渡河。求法和初值一样,向右扫描。

一个经典的模型。一开始在向右扫描时有疏漏,wa了几次...

 

 

codes:

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值