身材管理(三维背包)
Description
Input
Output
Sample
代码
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt(); // 输入n的值int money = scanner.nextInt();int v = scanner.nextInt();int price[] = new int[n];int capacity[] = new int[n];int happy[] = new int[n];for(int i =0;i<n;i++){price[i]=scanner.nextInt();capacity[i] = scanner.nextInt();happy[i] = scanner.nextInt();}System.out.println(calculate(price,capacity,happy,money,v));}public static int calculate(int price[],int cap[],int happy[],int allMoney,int capacity){int dp[][][] = new int[price.length+1][capacity+1][allMoney+1];int n = price.length;for(int i = 1;i<=n;i++)for(int money =1;money<=allMoney;money++)for(int c = 1;c<=capacity;c++){if(cap[i-1]<=c&&price[i-1]<=money){dp[i][c][money] = Math.max(dp[i-1][c-cap[i-1]][money-price[i-1]]+happy[i-1],dp[i-1][c][money]);}else {dp[i][c][money] = dp[i-1][c][money];}}return dp[n][capacity][allMoney];}}
思路
三维只是在二维上多了个嵌套的约束条件