POJ 1860(判定正圈)

Bellman_ford

Program P1860;
var
   n,m,i,j,s:longint;
   v:double;
   flag:boolean;
   d:array[1..100] of double;
   x,y:array[1..100] of longint;
   map:array[1..100,1..4] of double;
procedure relax(i:longint);
begin
   if (d[y[i]]<(d[x[i]]-map[i,2])*map[i,1]) then
   begin
      d[y[i]]:=(d[x[i]]-map[i,2])*map[i,1];
      flag:=false;
   end;
   if (d[x[i]]<(d[y[i]]-map[i,4])*map[i,3]) then
   begin
      d[x[i]]:=(d[y[i]]-map[i,4])*map[i,3];
      flag:=false;
   end;

end;
procedure bell_ford;
var
   i,j:longint;
begin
   for i:=1 to n do
   begin
      flag:=true;
      for j:=1 to m do relax(j);
      if flag then break;
   end;
   if flag then writeln('NO')
   else writeln('YES');
end;
begin
   while not seekeof do
   begin
      readln(n,m,s,v);
      fillchar(d,sizeof(d),0);
      d[s]:=v;
      for i:=1 to m do
      begin
         readln(x[i],y[i],map[i,1],map[i,2],map[i,3],map[i,4]);
      end;
      bell_ford;
   end;
end.