Cod sursa(job #1180349)
Utilizator | Data | 30 aprilie 2014 15:41:17 | |
---|---|---|---|
Problema | Distante | Scor | 0 |
Compilator | fpc | Status | done |
Runda | Arhiva de probleme | Marime | 3.19 kb |
Program dijkstra;
type lista = ^celula;
celula = record
nod : longint;
cost : longint;
pred : lista;
end;
const INF=200000000;
var Lda : array [1..50005] of lista;
r :lista;
H,D,D1: array [1..50005] of longint;
n,i,a,b,c,v,k,aux1,p,lungime,aux,ok,l,f,t,s,m: longint;
b1,b2 : array[0..1 shl 17] of char;
procedure coboara(k : longint);
var fiu,aux : longint;
begin
repeat
fiu:=0;
if k*2<=lungime then
begin
fiu:=k*2;
if (k*2<lungime) then
if (D[H[k*2+1]]<D[H[k*2]]) then fiu:=k*2+1;
if D[H[fiu]]>=D[H[k]] then fiu:=0
end;
if fiu<>0 then begin
aux:=H[k];
H[k]:=H[fiu];
H[fiu]:=aux;
k:=fiu;
end;
until fiu=0;
end;
begin
assign(input,'distante.in'); settextbuf(input,b1); reset(input);
assign(output,'distante.out'); settextbuf(output,b2); rewrite(output);
readln(t);
for f:=1 to t do begin
readln(n,m,s); aux1:=n;
for l:=1 to n do read(D1[l]);
for i:=1 to m 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;
H[1]:=s;
lungime:=1;
while lungime>0 do begin
v:=H[1];
r:=lda[v];
H[1]:=H[lungime];
lungime:=lungime-1;
coboara(1);
while r<>nil do begin
if D[v]+r^.cost<D[r^.nod] then begin
D[r^.nod]:=D[v]+r^.cost;
lungime:=lungime+1;
H[lungime]:=r^.nod;
k:=lungime;
while (k>1) and (D[H[k]]<D[H[k div 2]]) do begin
aux:=H[k];
H[k]:=H[k div 2];
H[k div 2]:=aux;
k:=k div 2;
end;
end;
r:=r^.pred;
end;
end;
for i:=1 to aux1 do
if D[i]<>D1[i] then begin
ok:=1;
writeln('NU');
break;
end;
if ok=0 then writeln('DA');
ok:=0;
end;
close(input);
close(output);
end.