题解 | 【模板】二维费用背包
【模板】二维费用背包
https://www.nowcoder.com/practice/84b88177894c4c82980017e6b4a15fb3
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cnt = in.nextInt();
int Tcap = in.nextInt();
int Hcap = in.nextInt();
int[][] objs = new int[cnt][2];
long[] vals = new long[cnt];
for(int i = 0; i < cnt; i++){
objs[i][0] = in.nextInt();
objs[i][1] = in.nextInt();
vals[i] = in.nextLong();
}
long[][] dp = new long[Tcap+1][Hcap+1];
for(int i = 0; i < cnt; i++){
for(int t = Tcap; t >= objs[i][0]; t--){
for(int h = Hcap; h >= objs[i][1]; h--){
dp[t][h] = Math.max(dp[t][h], dp[t-objs[i][0]][h-objs[i][1]]+vals[i]);
}
}
}
System.out.println(dp[Tcap][Hcap]);
}
}
查看30道真题和解析
