Cod sursa(job #271219)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 4 martie 2009 23:18:32
Problema Distante Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.14 kb
const inf=1000000;
const nmax=50000;
type muchie=record
                 x,y,cost:longint;
                 end;
var f,h:text;
ok:boolean;
g:array[1..2*nmax] of muchie;
df,d,p:array[1..nmax]of longint;
s:array[1..nmax]of 0..1;
c,x1,x,y,min,n,i,m,j,k:longint;
i1,t:byte;

procedure divid(st,dr:longint);
var p,i,j:longint;
aux:muchie;
begin
i:=st;
j:=dr;
p:=g[(st+dr)div 2].cost;
while (i<=j) do
   begin
     while (g[i].cost<p) do i:=i+1;
     while (g[j].cost>p) do j:=j-1;
     if i<=j then begin
             aux:=g[i];
             g[i]:=g[j];
             g[j]:=aux;
             i:=i+1;
             j:=j-1;
             end;
   end;
   if st<j then divid(st,j);
   if dr>i then divid(i,dr);
end;

begin
assign(f,'distante.in');
reset(f);
assign(h,'distante.out');
rewrite(h);
readln(f,t);

for i1:=1 to t do
begin
   readln(f,n,m,x1);
   for i:=1 to n do
     read(f,df[i]);
    for i:=1 to m do
      begin
        read(f,x,y,c);
        g[i].x:=x;
        g[i].y:=y;
        g[i].cost:=c;
        if x=x1 then d[y]:=c;
        if y=x1 then d[x]:=c;
      end;
     divid(1,m);
     for i:=1 to n do
        if (x1<>i) and (d[i]=0) then d[i]:=inf;
     repeat
       ok:=true;
       for i:=1 to m do
         if d[g[i].y]>(d[g[i].x]+g[i].cost) then
                begin
                  d[g[i].y]:=d[g[i].x]+g[i].cost;
                  ok:=false;
                end;
      until ok;
      repeat
       ok:=true;
       for i:=1 to m do
         if d[g[i].x]>(d[g[i].y]+g[i].cost) then
                begin
                  d[g[i].x]:=d[g[i].y]+g[i].cost;
                  ok:=false;
                end;
      until ok;
    for i:=1 to n do
      if d[i]=inf then d[i]:=0;
    for i:=1 to n do
     if (d[i]<>df[i]) then
                    begin
                      ok:=false;
                      break;
                      end;

     if i1<>t then begin if ok then writeln(h,'DA')
                        else writeln(h,'NU');end
             else if ok then write(h,'DA')
                        else write(h,'NU');
     for i:=1 to n do
       d[i]:=0;
end;
close(h);
end.