Pagini recente » Cod sursa (job #2555193) | Cod sursa (job #2806180) | Cod sursa (job #2559006)
var t:array[1..50005,1..50005] of int64;
v,viz,TT:array[1..50005] of int64;
a,b,x,min,mx:int64;
n,m,i,j,poz:longint;
begin
assign(input,'dijkstra.in'); reset(input);
assign(output,'dijkstra.out'); rewrite(output);
readln(n,m);
for i:=1 to m do
begin
readln(a,b,x);
t[a,b]:=x
end;
mx:=1000000000;
for i:=1 to n do
for j:=1 to n do
if i=j then t[i,j]:=0 else
if t[i,j]=0 then t[i,j]:=mx;
viz[1]:=1;
for i:=2 to n do
begin
v[i]:=t[1,i];
if t[1,i]<mx then TT[i]:=1
end;
for i:=1 to n-1 do
begin
min:=mx;
for j:=1 to n do
if (v[j]<min) and (viz[j]=0) then
begin
min:=v[j];
poz:=j
end;
viz[poz]:=1;
for j:=1 to n do
if (viz[j]=0) and (v[poz]+t[poz,j]<v[j]) then
begin
v[j]:=v[poz]+t[poz,j];
TT[j]:=poz
end
end;
for i:=2 to n do write(v[i],' ');
close(input);
close(output)
end.