Test instructions: There are n rooms, Superman starting from the shortest room, jumping down the current room is taller and closest to the current height of the room (that is, according to the height of the room to jump), but Superman jumps there is a horizontal distance limit D, he jumps the horizontal distance <=d. Now give you the height of each room is their relative position, you cannot change the relative position of the house, but can move the room horizontally, so that the shortest room and the highest room of the horizontal distance maximum. Superman can't jump to the last room, no matter how it moves. Output-1
Analysis: This problem is a differential constraint system
First you have to understand that the problem is the maximum value, then each inequality is converted to x-y<=k form, and then even a y->x weighted value of the side of K.
Set di to the coordinates of the first point.
First the height of each building from small to large order, set num[i] for the lower floor of the corresponding subscript, then you can get D[num[i+1]]-d[num[i]]<=d
But it's obviously not enough to have this inequality, so it's easy to think of D[i+1]-d[i]<=d d[i]-d[i-1]<=0
The shortest path from min (Num[1],num[n]) to Max (Num[1],num[n]) is the answer.
Note Array to open int64 or long long
Code
Const Maxn=1000;var A,B,LAST:ARRAY[0..MAXN] of Longint; D:ARRAY[0..MAXN] of Int64; V:ARRAY[1..MAXN] of Boolean; STATE:ARRAY[1..MAXN*500] of Longint; SIDE:ARRAY[1..MAXN*10] of record x,y,z,next:longint; End E,n,m,s,t,l,q:longint;procedure qsort (l,r:longint); var i,j,k:longint;begin if L>=r then exit; I:=l; J:=r; k:=a[(i+j) Div 2]; Repeat while A[i]<k do Inc (i); While A[j]>k do Dec (j); If I<=j then begin a[0]:=a[i];a[i]:=a[j];a[j]:=a[0]; B[0]:=B[I];B[I]:=B[J];B[J]:=B[0]; Inc (I);d EC (j); End Until i>j; Qsort (I,R); Qsort (L,J); end;procedure Add (X,y,z:longint); Begin Inc (E); Side[e].x:=x; Side[e].y:=y; Side[e].z:=z; SIDE[E].NEXT:=LAST[X]; Last[x]:=e;end;function min (x,y:longint): Longint;begin if X<y then exit (x) Else exit (y); End;function Max (x,y:l Ongint): Longint;begin if X>y then exit (x) Else exit (y); end;procedure init;var I:longint;begin e:=0; Fillchar (last), 0 (last,sizeof); READLN (N,M); For I:=1To n do begin read (A[i]); B[i]:=i; End READLN; Qsort (1,n); S:=min (B[1],b[n]); T:=max (B[1],b[n]); For i:=1 to N-1 do begin Add (min (b[i],b[i+1]), Max (b[i],b[i+1]), m); Add (i+1,i,-1); Add (i,i+1,m); End;end;procedure Spfa;var Head,tail,i,u:longint;begin for i:=1 to N-1 does if ABS (B[i]-b[i+1]) >m THEN BEGIN Writeln (' case ', L, ': ',-1); Exit End Fillchar (d,sizeof (d), $7f Div 3); d[s]:=0; Fillchar (v,sizeof (v), true); V[s]:=false; head:=0; Tail:=1; State[1]:=s; Repeat Inc (head); U:=state[head]; I:=last[u]; While I>0 does with Side[i] does begin if d[x]+z<d[y] then begin d[y]:=d[x]+z; If v[y] then begin v[y]:=false; Inc (tail); State[tail]:=y; End End I:=next; End V[u]:=true; Until head>=tail; Writeln (' case ', L, ': ', d[t]); End;begin readln (q); For l:=1 to Q do BEGIN init; SPFA; End;end.
HDU 3440 Housu man differential restraint system