解题思路:
动态规划
多重背包问题需要在01背包问题(不重复)的基础上多加一层循环进行遍历,并且dp[ j ]的式子也需要修改
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int V = scan.nextInt();
int[] volume = new int[N];
int[] value = new int[N];
int[] s = new int[N];
for (int i = 0; i < N; i++) {
volume[i] = scan.nextInt();
value[i] = scan.nextInt();
s[i] = scan.nextInt();
}
int[] dp = new int[V + 1];
for (int i = 0; i < N; i++) {
//倒序遍历,确保不会重复
for (int j = V; j >= volume[i]; j--) {
//因为至少一个,所以 k 从一开始取
for (int k = 1; k <= s[i] && k * volume[i] <= j; k++)
dp[j] = Math.max(dp[j], dp[j - k * volume[i]] + k * value[i]);
}
}
System.out.println(dp[V]);
}
}