Pagini recente » Cod sursa (job #2667949) | Cod sursa (job #2273352) | Cod sursa (job #2635943) | Cod sursa (job #792972) | Cod sursa (job #382140)
Cod sursa(job #382140)
{distante pagina 167}
const inf=maxlongint div 2;
type vector=array[1..50000] of longint;
bool=array[1..50000] of boolean;
muchie=record
x,y,c:longint;
end;
var t:byte;
function exista(x:longint; vezi:vector; var marc:bool; n:longint):boolean;
var i:longint;
begin
exista:=false;
for i:=1 to n do
if (vezi[i]=x) and not marc[x] then
begin
marc[vezi[i]]:=true;
exista:=true;
exit;
end;
end;
procedure rezolvare(n,m,s:longint);
var d,vezi:vector;
u:array[1..100000] of muchie;
marc:bool;
i:longint;
ok:boolean;
begin
for i:=1 to n do
begin
read(vezi[i]);
d[i]:=inf;
end;
fillchar(marc,sizeof(marc),false);
for i:=1 to m do read(u[i].x,u[i].y,u[i].c);
d[s]:=0;
ok:=true;
while ok do
for i:=1 to m do
if d[u[i].y]>d[u[i].x]+u[i].c then
begin
d[u[i].y]:=d[u[i].x]+u[i].c;
ok:=false;
end;
for i:=1 to n do
if d[i]=inf then d[i]:=0;
for i:=1 to n do
if not exista(d[i],vezi,marc,n) then
begin
writeln('NU');
break;
end;
writeln('DA');
end;
procedure citire;
var i:byte;
n,m,s:longint;
begin
assign(input,'distante.in'); reset(input);
assign(output,'distante.out'); rewrite(output);
readln(t);
for i:=1 to t do
begin
readln(n,m,s);
rezolvare(n,m,s);
end;
close(input);
close(output);
end;
begin
citire;
end.