Cod sursa(job #632032)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 10 noiembrie 2011 09:27:19
Problema Algoritmul lui Dijkstra Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 1.13 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[r,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 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;
   end;
    for i:=1 to n do
    begin
     if i<>r then
      if t[i]<>0 then
       write (g,d[i],' ')
    end;
  close (F);
  close (G);
end.