动态规划-NASA的食物计划

在这里插入图片描述

输出格式

一个数 所能达到的最大卡路里(int范围内)

输入

320 350
4
160 40 120
80 110 240
220 70 310
40 400 220

输出

550
import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int vv[]=new int[1001];
        int ww[]=new int[1001];
        int k[]=new int[1001];
        int f[][]=new int[1001][1001];
        int v=scanner.nextInt();
        int w=scanner.nextInt();
        int t=scanner.nextInt();
        for (int i = 1; i <=t ; i++) {
            vv[i]=scanner.nextInt();
            ww[i]=scanner.nextInt();
            k[i]=scanner.nextInt();
        }
        for (int i = 1; i <=w ; i++) {
            for (int j = v; j >=vv[i] ; j--) {//体积
                for (int l =w ; l >=ww[i]; l--) {//体重
                    f[j][l]=Math.max(f[j][l],f[j-vv[i]][l-ww[i]]+k[i]);
                }
            }
        }
        System.out.println(f[v][w]);

    }
}
发布了89 篇原创文章 · 获赞 42 · 访问量 3674

猜你喜欢

转载自blog.csdn.net/weixin_43673156/article/details/104967058
今日推荐