-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwoConstraintsKnapsack.java
More file actions
28 lines (23 loc) · 995 Bytes
/
TwoConstraintsKnapsack.java
File metadata and controls
28 lines (23 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class TwoConstraintsKnapsack {
public static void main(String[] args) {
int[] value = {1, 2, 3};
int[] weight = {6, 1, 1};
int[] volume = {1, 5, 5};
System.out.println(solve(value.length, 5, 4, value, weight, volume));
}
public static int solve(int n, int max_weight, int max_volume, int[] value, int[] weight, int[] volume) {
int[][][] dp = new int[n + 1][max_weight + 1][max_volume + 1];
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= max_weight; ++j) {
for (int k = 0; k <= max_volume; ++k) {
dp[i][j][k] = dp[i - 1][j][k];
if (j >= weight[i - 1] && k >= volume[i - 1]) {
dp[i][j][k] = Math.max(dp[i][j][k],
dp[i - 1][j - weight[i - 1]][k - volume[i - 1]] + value[i - 1]);
}
}
}
}
return dp[n][max_weight][max_volume];
}
}