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 the districts, the northern and the southern, by a river flowing th Rough. The northern and southern districts is connected by N + 1 bidirectional ferry lanes, numbered 0 to N from west to east. Each ferry lane connects and docks in separate sides of the river. No, lanes share the same dock or cross. Today Arthur needs to deliver a-from-one dock to another. He knows the sailing time of each ferry lane and the time cost by walking from one dock to a adjacent one along the river Bank. Arthur wants to know, the minimum time his delivery would cost. Input The input consists of several test cases. The first line is consists an integer N (1≤n≤1,000,000). The second line consists of both pairs of integers describing the starting and finishing docks, where the first of each PAI R 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, adjacent docks on the northern bank fro 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, 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. 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 |
The main topic: as shown in the title of the figure, the north and South Shore each have n+1 a dock, numbered 0~n, give the two-way road between the dock and waterway (a dock corresponding to a waterway, waterway does not cross) the time required, from the point of dock to the point of the Dock to guide the Min.
Analysis: Can be solved with the shortest path, but N has 10^6, so it is necessary to use an efficient algorithm such as SPFA. Not necessarily the composition, because the edge is certain.
I'm using a DP of O (n).
If the given start coordinate is greater than the end coordinate, the interchange. F[I,0]F[I,1] represents the minimum time to go to the first and second points of the North Shore and the first I point of the South Bank, and 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, the shortest path from the starting point to the other side is first found. Just scan it to the left.
You cannot get the minimum value immediately after DP because you can cross the river right after the other side. The method is the same as the initial value, scanning to the right.
A classic model. At first there was an omission in the right scan, 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 does 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 and not seekeof do BEGIN init; dp End end.