USACO 3.3 Shopping Offers商店购物

这题考的就是编程复杂度好吧。然后果断五维DP不解释!

{
ID: ymwbegi1
PROG: shopping
LANG: PASCAL
}
var
  n,m,i,j,x,y,z,r:longint;
  flag:boolean;
  s1,w:array[1..100] of longint;
  s:array[1..100,1..100,1..2] of longint;
  f:array[0..5,0..5,0..5,0..5,0..5] of longint;
  num:array[1..1000] of longint;
  u,o:array[1..5] of longint;
  a:array[1..100,1..2] of longint;
begin
  assign(input,'shopping.in');
  assign(output,'shopping.out');
  reset(input);
  rewrite(output);
  readln(m);
  for i:=1 to m do
  begin
    read(s1[i]);
    for j:=1 to s1[i] do
      read(s[i,j,1],s[i,j,2]);
    readln(w[i]);
  end;
  readln(n);
  for i:=1 to n do
  begin
    readln(x,y,z);
    num[x]:=i;
    a[i,1]:=y;
    a[i,2]:=z;
  end;
  while u[1]<=a[1,1] do
  begin
    u[2]:=0;
    while u[2]<=a[2,1] do
    begin
      u[3]:=0;
      while u[3]<=a[3,1] do
      begin
        u[4]:=0;
        while u[4]<=a[4,1] do
        begin
          u[5]:=0;
          while u[5]<=a[5,1] do
          begin
            for i:=1 to n do
              f[u[1],u[2],u[3],u[4],u[5]]:=f[u[1],u[2],u[3],u[4],u[5]]+u[i]*a[i,2];
            for i:=1 to m do
            begin
              flag:=false;
              for j:=1 to s1[i] do
                if u[num[s[i,j,1]]]<s[i,j,2] then
                begin
                  flag:=true;
                  break;
                end;
              if flag then continue;
              for j:=1 to 5 do
                o[j]:=u[j];
              for j:=1 to s1[i] do
              begin
                r:=num[s[i,j,1]];
                o[r]:=u[r]-s[i,j,2];
              end;
              if f[o[1],o[2],o[3],o[4],o[5]]+w[i]<f[u[1],u[2],u[3],u[4],u[5]] then
                f[u[1],u[2],u[3],u[4],u[5]]:=f[o[1],o[2],o[3],o[4],o[5]]+w[i];
            end;
            inc(u[5]);
          end;
          inc(u[4]);
        end;
        inc(u[3]);
      end;
      inc(u[2]);
    end;
    inc(u[1]);
  end;
  writeln(f[a[1,1],a[2,1],a[3,1],a[4,1],a[5,1]]);
  close(input);
  close(output);
end.


你可能感兴趣的:(USACO 3.3 Shopping Offers商店购物)