Pagini recente » Cod sursa (job #506546) | Cod sursa (job #2474813) | Cod sursa (job #1574132) | Cod sursa (job #8468) | Cod sursa (job #693787)
Cod sursa(job #693787)
program dijkstra;
var f,g:text;
n,m,i,j,poz1,poz2,cost:longint;
a:array of array of integer;
d:array of longint;
viz:array of 0..1;
min,poz:longint;
begin
assign (f,'dijkstra.in'); reset (f);
assign (g,'dijkstra.out'); rewrite (g);
readln (f,n,m);
setlength (a,n+1);
setlength (d,n+1);
setlength (viz,n+1);
for i:=1 to n do
begin
setlength (a[i],n+1);
for j:=1 to n do
a[i,j]:=maxint;
end;
for i:=1 to m do
begin
readln (f,poz1,poz2,cost);
a[poz1,poz2]:=cost;
end;
for i:=2 to n do
d[i]:=a[1,i];
viz[1]:=1;
for i:=1 to n-1 do
begin
min:=maxlongint;
for j:=1 to n do
if viz[j]=0 then
if d[j]<min then
begin
min:=d[j]; poz:=j;
end;
viz[poz]:=1;
for j:=1 to n do
if viz[j]=0 then
if d[j]>d[poz]+a[poz,j] then
d[j]:=d[poz]+a[poz,j];
end;
for i:=2 to n do
begin
if d[i]=maxint then
d[i]:=0;
write (G,d[i],' ');
end;
close (F); close (g);
end.