I and OI
Past...

题意:有一辆车要从起点0,到终点L处,中间有若干个加油站.给出车的油箱容量200,每行驶1km耗油1L.给出加油站的坐标,以及每个加油站的油价.一开始油箱里有100L的油,到达终点时必须还有100L的油,求最少花多少钱在加油上.

分析:DP.对于到达终点时必须还有100L的油这个问题,只要将终点放远100米,设置为油价为0的加油站即可.

F[i,j]表示到达第i个加油站有油j升的最优值.到达一个加油站时,枚举加多少油就好了.

code:

var   f:array[0..110,0..210] of longint;
      g:array[0..110,0..210] of boolean;
      d,p:array[0..110] of longint;
      dis,n,i,j,k,c,ans:longint;

      function min(a,b:longint):longint;
      begin
            if a>b then exit(b); exit(a);
      end;


begin
      readln(dis);
      while not seekeof do
      begin
            inc(n);
            readln(d[n],p[n]);
            if d[n]>dis then break;
      end;
      if d[n]>dis then dec(n);
      inc(n);
      d[n]:=dis+100;
      p[n]:=0;

      f[0,100]:=0;
      g[0,100]:=true;
      for i:=0 to n-1 do
         for j:=0 to 200 do
         if g[i,j] then
         begin
               c:=j-(d[i+1]-d[i]);
               if c>=0 then
               begin
                     for k:=0 to 200 do
                        if k+c<=200 then
                          if not g[i+1,k+c] then
                          begin
                                f[i+1,k+c]:=f[i,j]+k*p[i+1];
                                g[i+1,k+c]:=true;
                          end
                          else f[i+1,k+c]:=min(f[i+1,k+c],f[i,j]+k*p[i+1])
                        else break;
               end;
         end;
      ans:=maxlongint;
      for i:=0 to 200 do
         if g[n,i] then ans:=min(ans,f[n,i]);

      if ans=maxlongint then writeln('Impossible')
      else writeln(ans);
end.
posted on 2011-08-13 12:35  exponent  阅读(318)  评论(0编辑  收藏  举报