tyvj P1326 - 剑人合一
P1326 - 剑人合一From lwz_th Normal (OI) 总时限:10s 内存限制:128MB |
|||||||
---|---|---|---|---|---|---|---|
|
Delphi语言: 高亮代码由发芽网提供
program tyvj1326;
const max=500000;
var q:array[1..max] of integer;
a:array[0..10000] of longint;
b:array[0..50000,1..3] of longint;
mark:array[1..10000] of boolean;
d:array[1..10000] of longint;
head,tail,n,m,x,y,i,k,now,dist,s,z:longint;
procedure push(k:longint);
begin
head:=head mod max+1;
q[head]:=k;
mark[k]:=true;
end;
function pop:longint;
begin
pop:=q[tail];
tail:=tail mod max+1;
end;
begin
fillchar(b,sizeof(b),0);
fillchar(a,sizeof(a),0);
readln(n);
readln(m);
for i:=1 to m do begin
readln(x,y,dist);
inc(z);b[z,1]:=y;b[z,2]:=dist;b[z,3]:=a[x];a[x]:=z;
end;
for i:=1 to n do d[i]:=maxlongint;
fillchar(mark,sizeof(mark),false);
head:=0;tail:=1;
d[1]:=0;
push(1);
while head>=tail do begin
k:=pop;s:=a[k];
while s<>0 do begin
now:=b[s,1];dist:=b[s,2];
if d[now]>d[k]+dist then begin
d[now]:=d[k]+dist;
if not mark[now] then push(now);
end;
s:=b[s,3];
end;
mark[k]:=false;
end;
writeln(d[n]);
end.
const max=500000;
var q:array[1..max] of integer;
a:array[0..10000] of longint;
b:array[0..50000,1..3] of longint;
mark:array[1..10000] of boolean;
d:array[1..10000] of longint;
head,tail,n,m,x,y,i,k,now,dist,s,z:longint;
procedure push(k:longint);
begin
head:=head mod max+1;
q[head]:=k;
mark[k]:=true;
end;
function pop:longint;
begin
pop:=q[tail];
tail:=tail mod max+1;
end;
begin
fillchar(b,sizeof(b),0);
fillchar(a,sizeof(a),0);
readln(n);
readln(m);
for i:=1 to m do begin
readln(x,y,dist);
inc(z);b[z,1]:=y;b[z,2]:=dist;b[z,3]:=a[x];a[x]:=z;
end;
for i:=1 to n do d[i]:=maxlongint;
fillchar(mark,sizeof(mark),false);
head:=0;tail:=1;
d[1]:=0;
push(1);
while head>=tail do begin
k:=pop;s:=a[k];
while s<>0 do begin
now:=b[s,1];dist:=b[s,2];
if d[now]>d[k]+dist then begin
d[now]:=d[k]+dist;
if not mark[now] then push(now);
end;
s:=b[s,3];
end;
mark[k]:=false;
end;
writeln(d[n]);
end.