Pagini recente » Cod sursa (job #1356937) | Cod sursa (job #1134976) | Cod sursa (job #2319964) | Cod sursa (job #1325145) | Cod sursa (job #1178892)
Program dijkstra;
type lista = ^celula;
celula = record
nod : longint;
cost : longint;
pred : lista;
end;
const INF=200000000;
var Lda : array [1..300005] of lista;
r :lista;
Coada,D,D1: array [1..300051] of longint;
n,i,a,b,c,v,aux,p,beg,sf,t,j,s,ok: longint;
b1,b2 : array[0..1 shl 17] of char;
begin
assign(input,'distante.in'); settextbuf(input,b1); reset(input);
assign(output,'distante.out'); settextbuf(output,b2);rewrite(output);
readln(t);
for j:=1 to t do begin
readln(n,p,s); aux:=n;
for i:=1 to n do read(D1[i]);
for i:=1 to p do begin
readln(a,b,c);
new(r);
r^.nod:=b;
r^.cost:=c;
r^.pred:=lda[a];
lda[a]:=r;
end;
for i:=1 to n do D[i]:=INF;
D[s]:=0;
coada[1]:=s; beg:=1; sf:=1;
while beg<=sf do begin
v:=coada[beg];
r:=lda[coada[beg]];
while r<>nil do begin
if D[v]+r^.cost<D[r^.nod] then begin
D[r^.nod]:=D[v]+r^.cost;
sf:=sf+1;
coada[sf]:=r^.nod;
end;
r:=r^.pred;
end;
beg:=beg+1;
end;
for i:=2 to aux do
if D[i]<>D1[i] then begin
ok:=1;
writeln('NU');
break;
end;
if ok=0 then writeln('DA');
end;
close(output);
end.