Cod sursa(job #559944)

Utilizator boti12botiGal Botond boti12boti Data 18 martie 2011 11:02:34
Problema Algoritmul lui Dijkstra Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 1.47 kb
uses crt;
type mat=array[1..200,1..200] of shortint;
     vek=array[1..500] of shortint;
var e,jr:^vek; x:mat; i,j,a,b,c,n,m,kcs,l,cs:shortint; f:text;
procedure lol(var cs:shortint);
  var i,min,cs1:shortint; t:boolean;
  begin
    min:=l; t:=false;
    cs1:=0;
    for i:=1 to n do
      if (e^[i]<min)and(jr^[i]<>1) then begin min:=e^[i]; cs1:=i; end;
   if cs1<>0 then jr^[cs1]:=1;
   for i:=1 to n do
    if jr^[i]=0 then t:=true;
    if t and (cs1<>0) then  begin
    for i:=1 to n do begin
      if x[cs1,i]+min<e^[i] then begin e^[i]:=x[cs1,i]+min; {os[i]:=cs1;} end;
      end;
      cs:=cs1;
    lol(cs);
    end;
  end;
begin
  assign(f,'dijkstra.in');
  reset(f);
  readln(f,n,m);
  l:=0;
  new(jr); new(e);
  if m<>0 then begin
  for i:=1 to m do begin
        readln(f,a,b,c);
        if c<>0 then begin x[a,b]:=c; x[b,a]:=c; end
        else begin x[a,b]:=-1; x[b,a]:=-1; end;
        l:=l+c;
      end;
  close(f);
  l:=l+1;
  kcs:=1;
  for i:=1 to n do
    for  j:=1 to n do
      if x[i,j]=0 then x[i,j]:=l
      else if x[i,j]=-1 then x[i,j]:=0;
  for i:=1 to n do
   if i<>kcs then e^[i]:=x[kcs,i]
   else if i=kcs then e^[i]:=0
   else e^[i]:=l;
  for i:=1 to n do
   jr^[i]:=0;
  jr^[kcs]:=1;
  cs:=kcs;
 { for i:=1 to n do
    os[i]:=kcs;
    os[kcs]:=0;  }
  lol(cs);
  end;
  assign(f,'dijkstra.out');
  rewrite(f);
  for i:=2 to n do
   if e^[i]<>l then write(f,e^[i],' ')
   else write(f,0,' ');
  close(f);
end.