tyvj ["扫地"杯III day1]破坏基地
P2040 - ["扫地"杯III day1]破坏基地From liouzhou_101 Normal (OI) 总时限:10s 内存限制:128MB |
|||||||||
---|---|---|---|---|---|---|---|---|---|
|
Delphi语言: 高亮代码由发芽网提供
program tyvj2040;
var q:array[1..100000] of integer;
dist,d,r:array[1..10000] of longint;
a:array[0..10000] of longint;
b:array[0..3000000,1..3] of longint;
mark:array[0..10000] of boolean;
s,n,m,l,head,tail,now,k,i,j,x,y,z,dd:longint;
procedure qsort(head,tail:integer);
var i,j,x:integer;
begin
i:=head;j:=tail;x:=r[head];
while i<j do begin
while (i<j)and(r[j]>=x) do dec(j);
r[i]:=r[j];
while (i<j)and(r[i]<=x) do inc(i);
r[j]:=r[i];
end;
r[i]:=x;
if head<i-1 then qsort(head,i-1);
if tail>i+1 then qsort(i+1,tail);
end;
procedure push(k:longint);
begin
inc(head);q[head]:=k;
mark[k]:=true;
end;
function pop:longint;
begin
pop:=q[tail];
inc(tail);
end;
begin
fillchar(mark,sizeof(mark),false);
readln(n,m);z:=0;
for i:=1 to n do dist[i]:=maxlongint;
for i:=1 to m do begin
readln(x,y,dd);
inc(z);b[z,1]:=y;b[z,2]:=dd;b[z,3]:=a[x];a[x]:=z;
inc(z);b[z,1]:=x;b[z,2]:=dd;b[z,3]:=a[y];a[y]:=z;
end;
head:=0;tail:=1;
push(1);
dist[1]:=0;
while head>=tail do begin
k:=pop;s:=a[k];
while s<>0 do begin
now:=b[s,1];dd:=b[s,2];
if dist[now]>dist[k]+dd then begin
d[now]:=k;
dist[now]:=dist[k]+dd;
if not mark[now] then push(now);
end;
s:=b[s,3];
end;
mark[k]:=false;
end;
k:=n;l:=0;
while d[k]>1 do begin
inc(l);r[l]:=d[k];
k:=d[k];
end;
qsort(1,l);
writeln(l);
for i:=1 to l do writeln(r[i]);
end.