Cod sursa(job #158668)

Utilizator constantin02constantin constantin02 Data 13 martie 2008 19:25:50
Problema Distante Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.87 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,vv: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[tata]:=v;
  poz[v]:=tata;
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[fiu]]) do
    begin
      aux:=H[tata];
      H[tata]:=H[fiu];
      poz[H[fiu]]:=tata;
      H[fiu]:=aux;
      poz[aux]:=fiu;
      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;
      new(p);
      p^.info:=a;
      p^.cost:=c;
      p^.next:=list[b];
      list[b]:=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>2 do
    begin
      min:=H[1];
      H[1]:=H[t];
      dec(t);
      down(1,t);
      p:=list[min];
      vv[min]:=1;
      while p<>nil do
        begin
          if (D[p^.info]>D[min]+p^.cost)and(vv[p^.info]=0) then
            begin
              D[p^.info]:=D[min]+p^.cost;
              if D[p^.info]<>DD[p^.info] then
                begin
                  for i:=1 to n do vv[i]:=0;
                  dijkstra:=0;
                  exit;
                end;
              up(poz[p^.info],t);
            end;
          p:=p^.next;
        end;
    end;
  D[ns]:=inf;
  for i:=1 to n do vv[i]:=0;
  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.