[动态规划]Pku3377--Ferry Lanes

来源:互联网 发布:有微信骚扰软件吗 编辑:程序博客网 时间:2024/05/16 18:14

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

 

Ferry Lanes
Time Limit: 2000MS Memory Limit: 131072KTotal 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

40 0 1 41 3 5 73 5 1 3 71 3 7 50

Sample Output

17

Source

POJ Monthly--2007.09.09, Dagger

 

题目大意:如题目的图中所示,南北岸各有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:

 

 

原创粉丝点击