Cod sursa(job #673752)
Program distante;
type tip=record
x,y,t:longint;
end;
var a:array [0..100000] of tip;
v,c:array [0..50000] of longint;
b1,b2:array [1..1 shl 17] of char;
n,m,i,t,j,s,k:longint;
ok,okk:boolean;
fi,fo:text;
begin
assign(fi,'distante.in');
assign(fo,'distante.out');
settextbuf(fi,b1);
settextbuf(fo,b2);
reset(fi); rewrite(fo);
readln(fi,t);
for j:=1 to t do begin
readln(fi,n,m,s);
for i:=1 to n do read(fi,c[i]); readln(fi);
for i:=1 to m do begin inc(k);
readln(fi,a[k].x,a[k].y,a[k].t);
inc(k); a[k].x:=a[k-1].y; a[k].y:=a[k-1].x; a[k].t:=a[k-1].t;
end;
for i:=1 to n do v[i]:=-1;
v[s]:=0; ok:=true;
while ok do begin
ok:=false;
for i:=1 to 2*m do
if (v[a[i].x]<>-1) and ((v[a[i].y]=-1) or (v[a[i].y]>v[a[i].x]+a[i].t)) then
begin
v[a[i].y]:=v[a[i].x]+a[i].t;
ok:=true;
end;
end;
okk:=true;
for i:=1 to n do
if v[i]=-1 then begin if c[i]<>0 then okk:=false; end
else if c[i]<>v[i] then okk:=false;
if okk then writeln(fo,'DA')
else writeln(fo,'NU');
end;
close(fo);
end.