Cod sursa(job #632038)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 10 noiembrie 2011 09:42:58
Problema Algoritmul lui Dijkstra Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 1.18 kb
program graf;
var f,g:text;
    n,i,j,poz,r,min:integer;
    m:longint;
    v1,v2,v3:integer;
    a:array of array 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);
 setlength (a,n+1);
 for i:=1 to n do
 begin
 setlength (a[i],n+1);
  for j:=1 to n do
  begin
   a[i,j]:=maxint;
   a[i,i]:=0;
  end;
  end;
  for i:=1 to m do
  begin
   readln (f,v1,v2,v3);
   a[v1,v2]:=v3;
  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]=maxint then
      write (g,'0')
      else
      if t[i]<>0 then
       write (g,d[i],' ');
    end;
  close (F);
  close (G);
end.