Pagini recente » Cod sursa (job #2189767) | Cod sursa (job #410852) | Cod sursa (job #2284804) | Cod sursa (job #2854253) | Cod sursa (job #1172285)
Program dijkstra;
type lista = ^celula;
celula = record
nod : longint;
cost : longint;
pred : lista;
end;
const INF=200000000;
var Lda : array [1..300005] of lista;
r :lista;
H,D: array [1..300051] of longint;
n,i,a,b,c,v,aux,p,beg,sf: longint;
begin
assign(input,'dijkstra.in'); reset(input);
assign(output,'dijkstra.out'); rewrite(output);
readln(n,p); aux:=n;
for i:=1 to p do begin
readln(a,b,c);
new(r);
r^.nod:=b;
r^.cost:=c;
r^.pred:=lda[a];
lda[a]:=r;
end;
for i:=2 to n do D[i]:=INF;
H[1]:=1;
beg:=1;
sf:=1;
while beg<=sf do begin
v:=H[beg];
r:=lda[H[beg]];
while r<>nil do begin
if D[v]+r^.cost<D[r^.nod] then begin
D[r^.nod]:=D[v]+r^.cost;
sf:=sf+1;
h[sf]:=r^.nod;
end;
r:=r^.pred;
end;
beg:=beg+1;
end;
for i:=2 to aux do
if D[i]=INF then write(0,' ')
else write(D[i],' ');
close(output);
end.