Algorithm programming (java) # dynamic programming problem and knapsack problem

Copyright: Author: xp_9512 Source: CSDN Copyright: This article is a blogger original article, reproduced, please attach Bowen link! Original: https://blog.csdn.net/qq_40981804/article/details/89512230

Yue Yue fled together a program with Jojo

Problem Description

Yue Yue with Jojo fled together, there are many things to put Jojo's inside the package, but limited package size, so we can only put important items in it. Given the number of this kind of article, volume, value value now, I hope you can calculate how to make the maximum value of the combination backpack, and output this value, Jojo would like to thank you very much.

Input Format

(1) the first line has two integers, the number n of kinds of goods and a backpack load volume v.

Number m (2) 2 to row n + 1 lines each an integer of 3, for the i-th item, the volume of w, the value of s. .

Output Format

Only contains an integer, the largest sum is the value of goods can get.

Scale data

1<=v<=500

1<=n<=2000

1<=m<=5000

1<=w<=20

1<=s<=100

Example 1

  • Entry

    2 10
    3 4 3
    2 2 5
    
  • Export

    13
    
  • DESCRIPTION
    selected from a first, two second, the result is 3x1 + 5x2 = 13.

Problem-solving ideas

Dynamic programming problem and knapsack problem, the first thought to create a dp array of items for maximum storage value of each volume can be obtained.
Dynamic programming formulation:
Formula I: maximum value of items vol volume is obtainable as max (dp [vol - volume of the current input w [i]] + current input value s [i], dp [vol ]).
Formula 2: the maximum volume value can be obtained when the article is vol max (DP [vol - K V [I]] K + S [I], DP [vol]). DP // [Vol - K V [I]] K + S [I]

An example of a program,


import java.util.*;

public class Main {
    private static final int N_MAX = 2005;
    private static final int V_MAX = 505;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //n为物品的种数,v为背包装载体积
        int n = sc.nextInt(), v = sc.nextInt();
        //第i种物品的数量nums、体积vs、价值ws。
        int[] vs = new int[N_MAX], ws = new int[N_MAX], nums = new int[N_MAX];
        for (int i=1; i<=n; i++) {
            nums[i] = sc.nextInt(); vs[i] = sc.nextInt(); ws[i] = sc.nextInt();
        }
        //最大的物品价值总和为dp
        int[] dp = new int[V_MAX];
        //i物品的种类
        for (int i=1; i<=n; i++) {
        	//j为,背包的体积
            for (int j=v; j>=vs[i]; j--) {
            	//k为放入背包的数量
                for (int k=1; k <= nums[i] && k*vs[i]<=j; k++) {
                	//dp[j]为现有背包的价值,dp[j-k*vs[i]] 存放k个物品i后背包的价值
                    dp[j] = Math.max(dp[j], dp[j-k*vs[i]] + k* ws[i]);
                }
            }
        }
        System.out.println(dp[v]);
        sc.colse();
    }
}

Program Example II.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        //物品的种类数量
        int n = cin.nextInt();
        //背包的体积
        int v = cin.nextInt();
        cin.nextLine();
        //最大的物品价值总和为dp
        int[] dp = new int[v+1];
        //第i种物品的数量m,体积w,价值s
        int[] m = new int[n];
        int[] w = new int[n];
        int[] s = new int[n];
        for(int i = 0;i < n;i++) {
            m[i] = cin.nextInt();
            w[i] = cin.nextInt();
            s[i] = cin.nextInt();
            cin.nextLine();
            int sum = 0;
            //数量不能超过m[i]
            for(int j=1;j<=m[i];j++) {
                sum += w[i];
                //体积不能超过v
                if(sum > v) break;
                //计算每个体积下,存放一种物品所能达到的最大价值
                for(int k = v; k >= w[i]; k--) {
                    dp[k] = dp[k]>=dp[k-w[i]]+s[i]?dp[k]:dp[k-w[i]]+s[i];
                }
//                //调试用的
//                for(int z = 1;z<= 10;z++){
//                    System.out.print(z +"-"+ dp[z] +",");
//                }
//                    System.out.println(sum);
            }
        }
        System.out.println( dp[v]);
        cin.close();
    }
}





Guess you like

Origin blog.csdn.net/qq_40981804/article/details/89512230