Mai intai trebuie sa te autentifici.
Cod sursa(job #631648)
Utilizator | Data | 9 noiembrie 2011 12:25:35 | |
---|---|---|---|
Problema | Algoritmul lui Dijkstra | Scor | 0 |
Compilator | fpc | Status | done |
Runda | Arhiva educationala | Marime | 1.12 kb |
program graf;
type natural=record
a,b,c:integer;
end;
var f,g:text;
n,m,i,j,poz,r,min:integer;
v:array [1..250000] of natural;
a:array [1..100,1..1000] of integer;
s,d,t:array [1..100] of integer;
begin
assign (F,'dijkstra.in'); reset (f);
assign (g,'dijkstra.out'); rewrite (g);
readln (f,n,m);
for i:=1 to n do
for j:=1 to n do
begin
a[i,j]:=maxint;
a[i,i]:=0;
end;
for i:=1 to m do
begin
readln (f,v[i].a,v[i].b,v[i].c);
a[v[i].a,v[i].b]:=v[i].c;
end;
r:=1;
s[1]:=1;
for i:=1 to n do
begin
d[i]:=a[1,i];
if i<>r then
if d[i]<maxint then
t[i]:=r;
end;
for i:=1 to n+1 do
begin
min:=maxint;
for j:=1 to n do
if s[j]=0 then
if d[j]<min then
begin
min:=d[j];
poz:=j;
end;
end;
s[poz]:=1;
for j:=1 to n+1 do
if s[j]=0 then
if d[j]>d[poz]+a[poz,j] then
begin
d[j]:=d[poz]+a[poz,j];
t[j]:=poz;
end;
for i:=1 to n+1 do
begin
if i<>r then
if t[i]<>0 then
write (g,d[i],' ')
end;
close (F);
close (G);
end.