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 are partitioned into two districts, the northern and the southern, by a river flowing th Rough. The northern and southern districts are connected by N + 1 bidirectional ferry, lanes 0 to N 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" by walking from one dock to a adjacent one along the river Bank. Arthur wants to know what are the minimum time his delivery'll cost. Input The input consists of several test cases. The ' a ' of each consists is an integer N (1≤n≤1,000,000). The second line consists of two pairs of integers describing the starting and finishing, where the R represents the district (0 means northern and 1 means southern) and the second the lane number. The third line contains N integers describing the time cost by walking between two adjacent on the Docks Bank northern M 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 POJ monthly--2007.09.09, Dagger |
Title: As shown in the picture, the north and south coast have n+1 dock, numbered 0~n, give the dock between the two-way access and waterway (a dock corresponding to a waterway, waterway does not intersect) time required, from the steering dock to the steering dock at least time-consuming.
Analysis: can use the shortest path to solve, but N has 10^6, so to use SPFA and other efficient algorithm to do. Not necessarily the composition, because the side is certain.
I'm using a DP of O (n).
Swap if the given starting point coordinates are greater than the endpoint coordinates. The f[i,0]f[i,1] represents the minimum time to go to the first point of the north bank and the first point of the South Bank, which is not difficult to obtain:
F[i,0]:=min (F[i-1,0]+cost,f[i-1,1]+cost+lane).
f[i,1] in the same vein.
Initially, we must first find the shortest path from the starting point to the opposite shore. Just scan the left to the right.
A DP cannot immediately get the minimum value because it can cross the river to the right. The method and the initial value, scan right.
A classic model. At the beginning of the scan to the right there are omissions, WA a few times ...
Codes:
var N,sx,ex,sp,ep,t:int64; F,cost:array[0..1000011,0..1] of Int64; LS,LX,RS,RX,L:ARRAY[0..1000011] of Int64; function min (a,b:int64): Int64; Begin if A>b then a:=b; Min:=a; End Procedure Init; var i:longint; X:int64; Begin READLN (n); If N=0 then halt; For i:=0 to n+1 do begin f[i,1]:=0; f[i,0]:=0; ls[i]:=0; rs[i]:=0; lx[i]:=0; rx[i]:=0; End READLN (SP,SX,EP,EX); Inc (SX); Inc (EX); If Sx>ex then begin T:=SX; Sx:=ex; ex:=t; T:=SP; SP:=EP; ep:=t; End For I:=1 to n do read (cost[i,0]); For i:=1 to N+1 do read (l[i); For I:=1 to n do read (cost[i,1]); For i:=sx-1 Downto 1 do begin ls[i]:=ls[i+1]+cost[i,0]; lx[i]:=lx[i+1]+cost[i,1]; End For i:=ex+1 to N+1 do begin rs[i]:=rs[i-1]+cost[i-1,0]; rx[i]:=rx[i-1]+cost[i-1,1]; End f[sx,sp]:=0; X:=L[SX]; For i:=sx-1 Downto 1 do x:=min (X,ls[i]+lx[i]+l[i]); F[sx,1-sp]:=x; End Procedure DP; var i:longint; Ans:int64; Begin for i:=sx+1 to ex do begin F[i,0]:=min (f[i-1,0]+cost[i-1,0],f[i-1,1]+l[i]+cost[i-1,1]); F[i,1]:=min (f[i-1,1]+cost[i-1,1],f[i-1, 0]+l[i]+cost[i-1,0]); End ANS:=F[EX,1-EP]+L[EX]; For i:=ex+1 to N+1 do Ans:=min (ans,f[ex,1-ep]+rs[i]+rx[i]+l[i)); Ans:=min (Ans,f[ex,ep]); Writeln (ANS); End Begin while does seekeof do BEGIN init; dp End end.