Pagini recente » Cod sursa (job #2502006) | Cod sursa (job #449182) | Cod sursa (job #1643355) | Cod sursa (job #2739269) | Cod sursa (job #2559015)
var t:array[1..50005,1..50005] of longint;
v:array[1..50005] of int64;
viz,TT:array[1..50005] of longint;
n,m,i,j,poz,a,b,x:longint;
min,mx:int64;
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
if v[i]=mx then write(0,' ') else write(v[i],' ');
close(input);
close(output)
end.