tyvj P2032 - 「Poetize9」升降梯上
P2032 - 「Poetize9」升降梯上From lydliyudong Normal (OI) 总时限:10s 内存限制:128MB |
|||||||
---|---|---|---|---|---|---|---|
|
Delphi语言: 高亮代码由发芽网提供
program poetize9p2;
type node=record
p,d:longint;
end;
var dist:array[1..1000,1..20] of int64;
mark:array[1..1000,1..20] of boolean;
w:array[1..20] of integer;
q:array[1..200000] of node;
n,m,head,tail,min,ans,k1,k2:int64;i,j:longint;
k:node;
procedure push(pp,dd:longint);
begin
inc(head);q[head].p:=pp;q[head].d:=dd;
mark[pp,dd]:=true;
end;
function pop:node;
begin
pop:=q[tail];
inc(tail);
end;
begin
readln(n,m);
fillchar(mark,sizeof(mark),false);
for i:=1 to m do begin
read(w[i]);
if w[i]=0 then min:=i;
end;
for i:=1 to n do
for j:=1 to m do dist[i,j]:=maxlongint;
dist[1,min]:=0;
head:=0;tail:=1;
push(1,min);
while head>=tail do begin
k:=pop;
for i:=1 to m do
if (w[i]+k.p<=n)and(w[i]+k.p>=1) then begin
k1:=w[i]+k.p;k2:=abs(k.d-i)+abs(2*w[i]);
if (dist[k1,i]>dist[k.p,k.d]+k2) then begin
dist[k1,i]:=dist[k.p,k.d]+k2;
if not mark[k1,i] then push(k1,i);
end;
end;
mark[k.p,k.d]:=false;
end;
ans:=maxlongint;
for i:=1 to m do
if dist[n,i]<ans then ans:=dist[n,i];
if ans=maxlongint then ans:=-1;
writeln(ans);
end.
type node=record
p,d:longint;
end;
var dist:array[1..1000,1..20] of int64;
mark:array[1..1000,1..20] of boolean;
w:array[1..20] of integer;
q:array[1..200000] of node;
n,m,head,tail,min,ans,k1,k2:int64;i,j:longint;
k:node;
procedure push(pp,dd:longint);
begin
inc(head);q[head].p:=pp;q[head].d:=dd;
mark[pp,dd]:=true;
end;
function pop:node;
begin
pop:=q[tail];
inc(tail);
end;
begin
readln(n,m);
fillchar(mark,sizeof(mark),false);
for i:=1 to m do begin
read(w[i]);
if w[i]=0 then min:=i;
end;
for i:=1 to n do
for j:=1 to m do dist[i,j]:=maxlongint;
dist[1,min]:=0;
head:=0;tail:=1;
push(1,min);
while head>=tail do begin
k:=pop;
for i:=1 to m do
if (w[i]+k.p<=n)and(w[i]+k.p>=1) then begin
k1:=w[i]+k.p;k2:=abs(k.d-i)+abs(2*w[i]);
if (dist[k1,i]>dist[k.p,k.d]+k2) then begin
dist[k1,i]:=dist[k.p,k.d]+k2;
if not mark[k1,i] then push(k1,i);
end;
end;
mark[k.p,k.d]:=false;
end;
ans:=maxlongint;
for i:=1 to m do
if dist[n,i]<ans then ans:=dist[n,i];
if ans=maxlongint then ans:=-1;
writeln(ans);
end.