题解 | 【模板】二维费用背包

【模板】二维费用背包

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]);
    }
}

全部评论

相关推荐

程序员小屁:帮你了查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务