Cod sursa(job #698386)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 29 februarie 2012 13:47:59
Problema Algoritmul lui Dijkstra Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 0.94 kb
program dijkstra;
type natural=record
 nod,cost:longint;
end;
var f,g:text;
    a:array of array of natural;
    d,c:array [1..50000] of longint;
    n,m,i,ps,pi,nr,wnod,wcost,x,y,z:longint;

begin
 assign (f,'dijkstra.in'); reset (f);
 assign (g,'dijkstra.out'); rewrite (g);
 readln (f,n,m);
 setlength (a,n+1,1);
 for i:=1 to m do
 begin
  readln (f,x,y,z);
  setlength (a[x],length (a[x])+1);
  a[x,0].nod:=a[x,0].nod+1;
  a[x,a[x,0].nod].nod:=y;
  a[x,a[x,0].nod].cost:=z;
 end;
 for i:=2 to n do
  d[i]:=maxlongint;
 ps:=0; pi:=1; c[1]:=1;
 while ps<pi do
 begin
  ps:=ps+1;
  nr:=c[ps];
  for i:=1 to a[nr,0].nod do
  begin
   wnod:=a[nr,i].nod;
   wcost:=a[nr,i].cost;
   if d[wnod]>d[nr]+wcost then
   begin
    pi:=pi+1;
    c[pi]:=wnod;
    d[wnod]:=d[nr]+wcost;
   end;
  end;
 end;
 for i:=2 to n do
  if d[i]=maxlongint then
   write (g,0,' ')
  else
   write (G,d[i],' ');
  close (f); close (g);
end.