AcWing 4. 多重背包问题 I
原题链接
简单
作者:
Douyi
,
2020-09-19 16:35:02
,
所有人可见
,
阅读 409
import java.util.Scanner;
public class Main {
static int N = 1010;
static int[] v = new int[N]; //物品体积
static int[] w = new int[N]; //物品价值
static int[] s = new int[N]; //物品个数
static int[][] f = new int[N][N]; //最大价值
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); //物品个数
int m = sc.nextInt(); //背包最大容量
for (int i = 1; i <= n; i ++ ) {
v[i] = sc.nextInt();
w[i] = sc.nextInt();
s[i] = sc.nextInt();
}
for (int i = 1; i <= n; i ++)
for (int j = 0; j <= m; j ++)
for (int k = 0; k <= s[i] && k * v[i] <= j; k ++) //不超过最大个数,同时不超过背包容量
f[i][j] = Math.max(f[i][j], f[i - 1][j - k*v[i]] + k*w[i]);
System.out.println(f[n][m]);
}
}