POJ 3259(负权回路)

内容目录

注意普通路径是双向的

program P3259;
var
   f,n,m,w,i,j:longint;
   s,e,t:longint;
   map:array[1..2500,1..3] of longint;
   wmap:array[1..300,1..3] of longint;
   d:array[1..2500] of longint;
   flag:boolean;
procedure relax(u,v,w:longint);
begin
   if (d[v]>d[u]+w) then
   begin
      d[v]:=d[u]+w;
      flag:=false;
   end;
end;
procedure bellman_ford;
var
   i,j:longint;
begin
   for i:=1 to n do
   begin
      flag:=true;
      for j:=1 to m do
      begin
         relax(map[j,1],map[j,2],map[j,3]);
         relax(map[j,2],map[j,1],map[j,3]);

      end;
      for j:=1 to w do relax(wmap[j,1],wmap[j,2],-wmap[j,3]);
      if flag then break;
   end;

   if flag then writeln('NO') else writeln('YES');
end;
begin
   read(f);
   while f>0 do
   begin
      fillchar(map,sizeof(map),0);
      fillchar(wmap,sizeof(wmap),0);
      fillchar(d,sizeof(d),0);

      read(n,m,w);
      for i:=1 to m do
      begin
         read(map[i,1],map[i,2],map[i,3]);
      end;
      for i:=1 to w do read(wmap[i,1],wmap[i,2],wmap[i,3]);
      bellman_ford;
      dec(f);
   end;
end.