Cod sursa(job #382595)

Utilizator cristinabCristina Brinza cristinab Data 13 ianuarie 2010 23:52:51
Problema Distante Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.54 kb
{distante pagina 167}
const inf=maxlongint div 2;

type vector=array[1..50000] of longint;
     muchie=record
            x,y,c:longint;
            end;

var t:byte;

procedure qsort(v:vector; l,r:longint);
var i,j,x,aux:longint;
begin
i:=l;
j:=r;
x:=v[(i+j) div 2];

repeat
 if v[i]<x then inc(i);
 if x<v[j] then dec(j);
 if i<=j then
    begin
    aux:=v[i];
    v[i]:=v[j];
    v[j]:=aux;
    inc(i);
    dec(j);
    end;
until i>j;

if i<r then qsort(v,i,r);
if l<j then qsort(v,l,j);
end;

procedure rezolvare(n,m,s:longint);
var d,vezi:vector;
    u:array[1..100000] of muchie;
    i:longint;
    ok:boolean;
begin
for i:=1 to n do
    begin
    read(vezi[i]);
    d[i]:=inf;
    end;


for i:=1 to m do read(u[i].x,u[i].y,u[i].c);

d[s]:=0;
ok:=true;
while ok do
      begin
      ok:=false;
      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:=true;
             end;
      end;

for i:=1 to n do
    if d[i]=inf then d[i]:=0;

qsort(vezi,1,n);
qsort(d,1,n);

for i:=1 to n do
    if vezi[i]<>d[i] then
       begin
       writeln('NU');
       exit;
       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.