NOIP2006T2 金明的预算方案
这道题可以用树形DP,也可以直接01背包,因为每个物品的附件不能再作为主件,所以选取每个物品就只有这几种可能的情况:不选这个物品、选取这个物品、选取这个物品及它的一个附件、选取这个物品及它的两个附件。很容易地想出来这是个背包问题。
用f[i,j]表示前i件物品用了j元钱获得的最大价值(即重要度与价值的成绩最大),那么状态转移方程是
f[i,j]=max{
f[i-1,j],//不选这件物品
f[i-1,j-cost[i,0]]+cost[i,0]*import[i,0],//只选择主件
f[i-1,j-cost[i,0]-cost[i,1]]+cost[i,0]*import[i,0]+cost[i,1]*import[i,1],//选择主件和附件1
f[i-1,j-cost[i,0]-cost[i,2]]+cost[i,0]*import[i,0]+cost[i,2]*import[i,2],//选择主件和附件2
f[i-1,j-cost[i,0]-cost[i,1]-cost[i,2]]+cost[i,0]*import[i,0]+cost[i,1]*import[i,1]+cost[i,2]*import[i,2],//选择主件和附件1、附件2
}
注意其中第二维的边界。
[pascal 代码]
VAR a,b:array[1..600,0..3]of longint; f:array[0..61,0..32001]of longint; n,m,tot:longint; Procedure init; var i,j,v,p,q:longint; begin readln(n,m); n:=n div 10; tot:=0; for i:=1 to m do begin readln(v,p,q); v:=v div 10; if q=0 then begin inc(tot); a[tot,0]:=v; b[tot,0]:=p; a[tot,3]:=i; end else begin for j:=1 to tot do if a[j,3]=q then break; if a[j,1]=0 then begin a[j,1]:=v;b[j,1]:=p; end else begin a[j,2]:=v;b[j,2]:=p; end; end; end; end; Procedure dp; var i,j,k:longint; begin fillchar(f,sizeof(f),0); for i:=1 to tot do for j:=0 to n do begin f[i,j]:=f[i-1,j]; if (j-a[i,0]>=0) and (f[i-1,j-a[i,0]]+a[i,0]*b[i,0]>f[i,j]) then f[i,j]:=f[i-1,j-a[i,0]]+a[i,0]*b[i,0]; if (j-a[i,0]-a[i,1]>=0)and (f[i-1,j-a[i,0]-a[i,1]]+a[i,0]*b[i,0]+a[i,1]*b[i,1]>f[i,j]) then f[i,j]:=f[i-1,j-a[i,0]-a[i,1]]+a[i,0]*b[i,0]+a[i,1]*b[i,1]; if (j-a[i,0]-a[i,2]>=0)and (f[i-1,j-a[i,0]-a[i,2]]+a[i,0]*b[i,0]+a[i,2]*b[i,2]>f[i,j]) then f[i,j]:=f[i-1,j-a[i,0]-a[i,2]]+a[i,0]*b[i,0]+a[i,2]*b[i,2]; if (j-a[i,0]-a[i,1]-a[i,2]>=0) then if f[i-1,j-a[i,0]-a[i,1]-a[i,2]]+a[i,0]*b[i,0]+a[i,1]*b[i,1]+a[i,2]*b[i,2]>f[i,j] then f[i,j]:=f[i-1,j-a[i,0]-a[i,1]-a[i,2]]+a[i,0]*b[i,0]+a[i,1]*b[i,1]+a[i,2]*b[i,2]; end; end; Begin init; dp; writeln(f[tot,n]*10); End.