AcWing 8. 【Java】二维费用的背包问题
原题链接
中等
作者:
tt2767
,
2020-01-12 21:25:43
,
所有人可见
,
阅读 763
/*
1. 状态定义: 和01背包一样,多了一个属性多一层循环
2. 状态计算:~
3. 边界: f[i][j] = 0
*/
import java.util.*;
public class Main{
void run(){
int N = jin.nextInt();
int V = jin.nextInt();
int M = jin.nextInt();
for (int i = 0 ; i < N ; i++){
v[i] = jin.nextInt();
m[i] = jin.nextInt();
w[i] = jin.nextInt();
}
for (int i = 0 ; i < N ; i++){
for (int j = M ; j >= m[i] ; j--){
for (int k = V; k >= v[i] ;k-- ){
f[j][k] = Math.max(f[j][k], f[j-m[i]][k-v[i]] + w[i]);
}
}
}
System.out.println(f[M][V]);
}
int maxN = 1003;
int[] v = new int[maxN];
int[] m = new int[maxN];
int[] w = new int[maxN];
int[][] f= new int[maxN][maxN];
Scanner jin = new Scanner(System.in);
public static void main(String[] args) {new Main().run();}
}