POJ 2983(差分约束-有无解)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nike0good/article/details/7874585

差分约束……


Program p2983;
var
   n,m,i,j,p:longint;
   c:char;
   flag:boolean;
   d:array[0..5000] of longint;
   w:array[1..100100,1..3] of longint;
procedure relax(v,u,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,k:longint;
begin
   flag:=true;
// w[i,3]=-1 w[i,2]-w[i,1]>0 ->w[i,1]-w[i,2]<0 ->w[i,1]-w[i,2]<=-1
// w[i,3]>-1 w[i,2]-w[i,1]=w[i,3] ->w[i,2]-w[i,1]<=w[i,3] w[i,1]-w[i,2]<=-w[i,3]
   for k:=1 to n+1 do
   begin
      flag:=true;
      for i:=1 to m do
      begin
         if w[i,3]=-1 then
         begin
            relax(w[i,1],w[i,2],-1);
         end
         else
         begin
            relax(w[i,2],w[i,1],w[i,3]);
            relax(w[i,1],w[i,2],-w[i,3]);
         end;

      end;
      if flag then break;
   end;

   if flag then writeln('Reliable')
   else writeln('Unreliable');
end;
begin
   while not seekeof do
   begin
      fillchar(d,sizeof(d),0);
      readln(n,m);
      for i:=1 to m do
      begin
         read(c);
         if (c='p') or (c='P') then
         begin
            readln(w[i,1],w[i,2],w[i,3]);
         end
         else
         begin
            readln(w[i,1],w[i,2]);
            w[i,3]:=-1;
         end;

      end;
      bellman_ford;

   end;
end.


展开阅读全文

没有更多推荐了,返回首页