Classic dynamic programming in java - 01 knapsack problem

Let’s explain today’s topic—the backpack problem—with an example of the Blue Bridge Cup

Topic 1924: Blue Bridge Cup Algorithm Improves VIP-01 Backpack

Time Limit: 1Sec Memory Limit: 128MB Commits: 4826 Solved: 1368

topic description

Given N items, each item has a weight W and a value V. You have a knapsack that can hold M weight. Ask how to pack it so that the value of the item is maximized. There is only one item for each item.

enter

The first line of input contains two integers n, m, which respectively represent the number of items and the weight that the backpack can hold. In the next N lines, there will be two numbers Wi and Vi in each line, indicating the weight and value of the item

output

Output 1 line containing an integer representing the maximum value.

Sample input copy

3 5
2 3
3 5
4 7

Sample output copy

8

After reading this question , we found that this is a very simple 01 backpack problem. In this question, we can easily find that the value of 3 and 5 is 8, which is the solution to this question, but this is compared to us "humans", who can quickly find the solution with our subjective judgment ability, while computers and machines It is not as powerful as us, and it is not as flexible. This is the most essential difference between machines and humans!

Ok, let’s get back to business, how should we answer the backpack problem?

 How to put these three items into the backpack? When the pointer points to any item (as shown in the figure below), we assume that the value of this item is dp[i][j] (i represents the index value of this item, here we 1 is Represents the first item, 2 represents the second item, and so on, j represents the quality of the schoolbag, where j is incremented from 1, representing the maximum value that the schoolbag can carry at a certain quality), then when the pointer points to When the item is retrieved, we will face two situations - take the item or not take the item. When the item is not taken away, dp[i][j]=dp[i-1][j], why is this so? Because if you don’t take the item, the quality b of the schoolbag will not change at all, and the value it carries is still the same as the value of the previous item i-1. When the item is taken away, dp[i][j]=dp[i-1][jW[i]]+V[i], why is this? We can split the understanding that when the item is taken away, the value of the backpack is only a difference of V[i], from dp[i][j]=dp[i-1][jW[i]]+V[i] It can be seen that dp[i][j] represents the current backpack, so will dp[i-1][jW[i]] represent the value of the schoolbag before taking the item? Obviously it is right, because i-1 represents the last item, and jW[i] also represents the quality of the schoolbag after taking the last item, so obviously dp[i-1][jW[i]] means that the item was not taken away The value of the previous schoolbag.

 Based on this, we can solve this problem by writing the following code:

package test;
import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();int m=sc.nextInt();
            int[] Wi=new int[n+1];    //创建Wi数组存储质量
            int[] Vi=new int[n+1];      //创建Vi数组存储某个物件所对应的价值
            for(int i=1;i<n+1;i++) {
                Wi[i]=sc.nextInt();
                Vi[i]=sc.nextInt();
            }
            int[][] dp=new int[n+1][m+1];   //创建dp数组存储背包所承受某个质量对应的价值
            for(int i=1;i<n+1;i++) {
                for(int j=1;j<m+1;j++) {
                    if(Wi[i]>j) {           //若当前物品的质量大于背包所承载的质量,则“不拿”该物品
                        dp[i][j]=dp[i-1][j];    
                    }
                    else {          //若当前物品的质量小于背包所承载的质量,则可以选择“拿” 或者 “不拿”,为了让书包价值更大,我们选其中的最大值
                        dp[i][j]=Math.max(dp[i-1][j], dp[i-1][j-Wi[i]]+Vi[i]);
                    }
                }
            }
            System.out.println(dp[n][m]);       //算法完成后,dp数组中的dp[n][m]是数组遍历完成的最后一个元素,也就是书包的最大价值
            
        }       
}

Guess you like

Origin blog.csdn.net/qq_49174867/article/details/123556173