Cod sursa(job #157042)

Utilizator Pepelea_FlaviuFlaviu Pepelea Pepelea_Flaviu Data 12 martie 2008 20:39:55
Problema Distante Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.61 kb
const nmax=50000;
      mmax=100000;
      inf=1 shl 30;
type pelem=^elem;
     elem=record
       info,cost:longint;
       next:pelem;
     end;
var fi,fo:text;
    a,b,S,n,c,M,T,i,j,ns,min:longint;
    H,poz,D,DD:array[1..nmax]of longint;
    list:array[1..nmax]of pelem;
    p:pelem;

procedure down(i,n:longint);
var v,tata,fiu:longint;
begin
  v:=H[i]; tata:=i; fiu:=i shl 1;
  while fiu<=n do
    begin
      if fiu<n then
        if D[H[fiu]]>D[H[fiu+1]] then fiu:=fiu+1;
      if D[v]>D[H[fiu]] then
        begin
          H[tata]:=H[fiu];
          poz[H[fiu]]:=tata;
          tata:=fiu;
          fiu:=fiu shl 1;
        end
      else fiu:=n+1;
    end;
  H[fiu]:=v;
  poz[v]:=fiu;
end;

procedure up(i,n:longint);
var fiu,tata,aux:longint;
begin
  fiu:=i; tata:=fiu shr 1;
  while (tata<>0)and(D[H[tata]]>D[H[i]]) do
    begin
      aux:=H[tata];
      H[tata]:=H[i];
      poz[H[i]]:=tata;
      H[i]:=aux;
      poz[aux]:=i;
      fiu:=tata;
      tata:=fiu shr 1;
    end;
end;

procedure readd;
var i:longint;
begin
  readln(fi,n,m,ns);
  for i:=1 to n do
    read(fi,DD[i]);
  for i:=1 to m do
    begin
      read(fi,a,b,c);
      new(p);
      p^.info:=b;
      p^.cost:=c;
      p^.next:=list[a];
      list[a]:=p;
    end;
end;

function dijkstra:byte;
begin
  readd;
  for i:=1 to N do
    begin
      H[i]:=i;
      poz[i]:=i;
      D[i]:=inf;
    end;
  D[ns]:=inf+1;
  p:=list[ns];
  while p<>nil do
    begin
      D[p^.info]:=p^.cost;
      p:=p^.next;
    end;
  t:=n;
  while t>1 do
    begin
      min:=H[1];
      H[1]:=H[t];
      dec(t);
      down(1,t);
      p:=list[min];
      while p<>nil do
        begin
          if D[p^.info]>D[min]+p^.cost then
            begin
              D[p^.info]:=D[min]+p^.cost;
              if D[p^.info]<>DD[p^.info] then
                begin
                  dijkstra:=0;
                  exit;
                end;
              up(poz[p^.info],t);
            end;
          p:=p^.next;
        end;
    end;
  D[ns]:=inf;
  for i:=1 to n do
    begin
      if D[i]<>inf then
        if D[i]<>DD[i] then
          begin
            dijkstra:=0;
            exit;
          end
        else
      else
       if DD[i]<>0 then
         begin
           dijkstra:=0;
           exit;
         end;
    end;
  dijkstra:=1;
end;

begin
  assign(fi,'distante.in'); reset(fi);
  assign(fo,'distante.out'); rewrite(fo);
  read(fi,T);
  for j:=1 to T do
    if dijkstra=1 then writeln(fo,'DA')
                  else writeln(fo,'NU');
  close(fi);
  close(fo);
end.