Cod sursa(job #251488)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 2 februarie 2009 20:24:04
Problema Algoritmul lui Dijkstra Scor 20
Compilator fpc Status done
Runda Arhiva educationala Marime 1.15 kb
const inf=1001;
const nmax=10000;
var f,g:text;
ok:boolean;
c:array[1..nmax,1..nmax]of integer;
d,p:array[1..nmax]of longint;
s:array[1..nmax]of 0..1;
x,y,min,n,i,m,j,k:longint;
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
  if i=j then c[i,j]:=0
  else c[i,j]:=inf;
for i:=1 to m do
begin
  readln(f,x,y,k);
  c[x,y]:=k;
  end;
close(f);
for i:=1 to n do
begin
d[i]:=c[1,i];
if d[i]<>inf then p[i]:=1
              else p[i]:=0;
end;
s[1]:=1;
p[1]:=0;

repeat
   ok:=false;
   min:=inf;
   for i:=1 to n do
        if (s[i]=0) and(d[i]<min) then
           begin
           min:=d[i];
           y:=i;
           ok:=true;
           end;
   if ok then begin
        s[y]:=1;
        for i:=1 to n do
           if (s[i]=0) and (d[i]>d[y]+c[y,i]) then
                          begin
                            d[i]:=d[y]+c[y,i];
                            p[i]:=y;
                          end;
         end;
   until not ok;
   for i:=2 to n do
     if d[i]<>inf then write(g,d[i],' ')
        else write(g,'0 ');
close(g);
end.