4. 多重背包问题 I
作者:
dsf2
,
2021-03-04 05:25:14
,
所有人可见
,
阅读 398
import java.util.*;
class Main {
public static int knapsackWithLimitedRepeat(int[] values, int[] weights, int[] counts, int capacity) {
int n = values.length; // value index start from 1, element at index 0 is 0, so as weight
// 2D array
int[][] dp = new int[n][capacity + 1];
for (int i = 1; i < n; i++)
for (int c = 0; c <= capacity; c++)
for (int j = 0; j <= counts[i] && j * weights[i] <= c; j++)
dp[i][c] = Math.max(dp[i][c], dp[i - 1][c - weights[i] * j] + values[i] * j);
return dp[n - 1][capacity];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int c = sc.nextInt();
int[] values = new int[n + 1];
int[] weights = new int[n + 1];
int[] counts = new int[n + 1];
for (int i = 1; i <= n; i++) {
weights[i] = sc.nextInt();
values[i] = sc.nextInt();
counts[i] = sc.nextInt();
}
System.out.println(knapsackWithLimitedRepeat(values, weights, counts, c));
}
}