Cod sursa(job #1173871)
Utilizator | Data | 21 aprilie 2014 00:02:21 | |
---|---|---|---|
Problema | Algoritmul lui Dijkstra | Scor | 50 |
Compilator | fpc | Status | done |
Runda | Arhiva educationala | Marime | 2.07 kb |
Program dijkstra;
type lista = ^celula;
celula = record
nod : longint;
cost : longint;
pred : lista;
end;
const INF=10000;
var Lda : array [1..50000] of lista;
r :lista;
H,D: array [1..50000] of longint;
n,i,a,b,c,v,k,aux1,p,lungime,aux: longint;
begin
assign(input,'dijkstra.in'); reset(input);
assign(output,'dijkstra.out'); rewrite(output);
readln(n,p); aux1:=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;
lungime:=1;
while lungime>0 do begin
v:=H[1];
r:=lda[v];
H[1]:=H[lungime];
lungime:=lungime-1;
while r<>nil do begin
if D[v]+r^.cost<D[r^.nod] then begin
D[r^.nod]:=D[v]+r^.cost;
lungime:=lungime+1;
H[lungime]:=r^.nod;
k:=lungime;
while (k>1) and (D[H[k]]<D[H[k div 2]]) do begin
aux:=H[k];
H[k]:=H[k div 2];
H[k div 2]:=aux;
k:=k div 2;
end;
end;
r:=r^.pred;
end;
end;
for i:=2 to aux1 do
if D[i]=INF then write(0,' ')
else write(D[i],' ');
close(input);
close(output);
end.