Cod sursa(job #764076)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 3 iulie 2012 23:06:54
Problema Distante Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.58 kb
program distante;
type natural=record
 nod,cost:longint;
end;
var f,g:text;
    t,j:byte;
    n,m,varf,i,x,y,z,ps,pi,nr,wcost,wnod:longint;
    c,viz,d:array [1..100000] of longint;
    a:array of array of natural;
    ok:boolean;
    bufin,bufout:array [1..65000] of byte;


begin
 assign (F,'distante.in'); reset (F);
 assign (G,'distante.out'); rewrite (g);
 settextbuf (f,bufin); settextbuf (g,bufout);
 readln (f,t);
 for j:=1 to t do
 begin
   readln (F,n,m,varf);
   for i:=1 to n do
    read (f,viz[i]);
   readln (f);
   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;
    setlength (a[y],length (a[y])+1);
    a[y,0].nod:=a[y,0].nod+1;
    a[y,a[y,0].nod].nod:=x;
    a[y,a[y,0].nod].cost:=z;
   end;
   for i:=1 to n do
    if i<>varf then
     d[i]:=maxlongint;
    ps:=0; pi:=1; c[1]:=varf;
    while ps<pi do
    begin
     inc(ps);
     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
       inc(pi); c[pi]:=wnod; d[wnod]:=d[nr]+wcost;
      end;
     end;
    end;
   ok:=true;
  for i:=1 to n do
  begin
   if d[i]=maxlongint then
    d[i]:=0;
   if d[i]<>viz[i] then
   begin
    ok:=false; break;
   end;
  end;
  if ok then
   writeln (G,'DA')
  else
   writeln (g,'NU');
  if j<>t then
  begin
   for i:=1 to n do
    a[i,0].nod:=0;
  end;
 end;
 close (F); close (G);
end.