Cod sursa(job #670602)

Utilizator MihaiBunBunget Mihai MihaiBun Data 29 ianuarie 2012 16:03:35
Problema Algoritmul lui Dijkstra Scor 40
Compilator fpc Status done
Runda Arhiva educationala Marime 1.59 kb
program kk;
type ref=^inr;
     inr=record
          nr:longint;
          cost:longint;
          adr:ref;
         end;
var f:text;
    lista,u:array[1..50000] of ref;
    q:ref;
    lung:array[1..50000] of longint;
    viz:array[1..50000] of 0..1;

    n,m,i,a,b,c,nod,nc,xx,nrviz,min:longint;

procedure adaug_in_lista(x,y,z:longint);
begin
   new(q);
   q^.nr:=y;
   q^.adr:=nil;
   q^.cost:=z;
   if lista[x]=nil then begin
                          lista[x]:=q;
                          u[x]:=q;
                        end
                   else begin
                         u[x]^.adr:=q;
                         u[x]:=q;

                        end;
end;

procedure dj;
begin
  nrviz:=1;
  nc:=1;
  viz[1]:=1;
  repeat
     min:=2140000000;
     q:=lista[nc];
     while q<>nil do
       begin
         xx:=lung[nc]+q^.cost;
         if xx<lung[q^.nr] then lung[q^.nr]:=xx;
         q:=q^.adr;
       end;
     for i:=1 to n do
       if viz[i]=0 then
          if lung[i]<min then begin
                               min:=lung[i];
                               nod:=i
                              end;
     nc:=nod;
     viz[nc]:=1;
     nrviz:=nrviz+1;
   until nrviz=n;
end;


begin
assign(f,'dijkstra.in');
reset(f);
readln(f,n,m);
for i:=1 to m do
  begin
     readln(f,a,b,c);
     adaug_in_lista(a,b,c);
  end;
lung[1]:=0;
for i:=2 to n do lung[i]:=2140000000;
dj;
close(f);
assign(f,'dijkstra.out');
rewrite(f);
for i:=2 to n do
  if lung[i]<2140000000 then write(f,lung[i],' ')
            else write(f,0,' ');
close(f);
end.