poj1201 Intervals

http://poj.org/problem?id=1201 

 

s[i]表示[0..i]中选取了几个整数

 

则可以得到一些不等式

s[a]-s[b-1]>=c  ([a,b]区间至少要选取c个)

s[i-1]-s[i]>=-1

s[i]-s[i-1]>=0

然后,使用差分约束系统做

 

Program poj1201;

type cord=record

             ne,po,da:longint;

          end;


var n,le,lr:longint;

    head:array[0..50000]of longint;

    link:array[1..1000000]of cord;

    dis:array[0..50000]of longint;

    h:array[1..1000000]of longint;

    f:array[0..50000]of boolean;



  Procedure add(x,y,z:longint);

   begin

      inc(le);

      with link[le] do

       begin

          po:=y;

          da:=z;

          ne:=head[x];

       end;

      head[x]:=le;

   end;


  Procedure init;

  var i,j,k,l:longint;

   begin

      readln(n);

      for i:=1 to n do

        begin

          readln(j,k,l);

          add(k,j-1,l);

          if k>lr then lr:=k;

        end;

      for i:=0 to lr do

       begin

        add(i,i+1,-1);

        add(i+1,i,0);

       end;

   end;


  Procedure spfa(x:longint);

  var i,j,k,t,w:longint;

   begin

      for i:=0 to lr do dis[i]:=-maxlongint div 10;

      dis[x]:=0;

      h[1]:=x;

      t:=0;w:=1;

      repeat

          inc(t);

          k:=h[t];

          f[k]:=false;

          i:=head[k];

          while i<>0 do

           begin

              if dis[k]+link[i].da>dis[link[i].po] then

                   begin

                       dis[link[i].po]:=dis[k]+link[i].da;

                       if f[link[i].po]=false then

                          begin

                              inc(w);

                              h[w]:=link[i].po;

                              f[link[i].po]:=true;

                          end;

                   end;

              i:=link[i].ne;

           end;

      until t=w;

   end;


  Procedure main;

   begin

       spfa(lr);

       writeln(dis[0]);

   end;


Begin

   assign(input,'input.in');reset(input);

   assign(output,'output.out');rewrite(output);

         init;

         main;

   close(Input);close(output);

End.

 

posted on 2016-03-02 21:06  Yesphet  阅读(202)  评论(0编辑  收藏  举报