题目来源
分析
1.可以分成三类分别做
s = -1:01背包
s = 0:完全背包
s = 1:多重背包
2.可以将三类都转换成多重背包
s = -1 => s = 1
s = 0 => s = 1000 (因为背包总体积最大为1000,所以无限件最多也就能用1000件)
具体代码
import java.util.Scanner;
public class Main{
public static void main(String[] args){
int maxN = 10001;
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int[] v = new int[maxN];
int[] w = new int[maxN];
int[] s = new int[maxN];
int p = 1;
for(int i = 1;i <= m;i++){
int v0 = sc.nextInt();
int w0 = sc.nextInt();
int s0 = sc.nextInt();
if(s0 == -1) s0 = 1;
if(s0 == 0) s0 = 1000;
int k = 1;
while(s0 >= k){
v[p] = k*v0;
w[p] = k*w0;
p++;
s0 -= k;
k *= 2;
}
if(s0 > 0){
v[p] = v0*s0;
w[p] = w0*s0;
p++;
}
}
int[] dp = new int[n+1];
for(int i = 1;i <= p;i++){
for(int j = n;j >= 0;j--){
dp[j] = dp[j];
if(j >= v[i])
dp[j] = Math.max(dp[j],dp[j-v[i]]+w[i]);
}
}
System.out.println(dp[n]);
}
}