01 backpack Problems

Source: Internet
Author: User

There are N items and a backpack with a capacity of V. The cost (volume) of the I-th item is C [I], and the value is W [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest. Obviously, this problem is characterized by the fact that there is only one item for each type, and you can choose to put it or not. I mainly focus on the dynamic planning method. There are already a lot of articles on this issue on the Internet, but I want to write down my own ideas and ideas, so I can understand this problem. Consider N items in sequence. For the maximum value F obtained by a backpack with a capacity of v when processing the I item, there is obviously the following recursive formula: IF the I-th item cost (volume) C [I]> v THEN I-th item cannot be added to the backpack, F [I] [v] = F [I-1] [v] whether the ELSE is added to the I-th item requires consideration of whether it is cost-effective after being added to the backpack, here we can think that this I-th item is preferred to a backpack, then F [I] [v] = max {F [I-1] [v], F [I-1] [v-C [I] + W [I]} Now Let's explain the most critical state transfer equation above! The current backpack capacity is v, which is about to process the I-th item. Obviously, there are two solutions with the optimal sub-structure:. if the I-th item is added to the backpack, load the I-th item to obtain the maximum value F [I] [v], it must be equal to the value of the I-item W [I] plus the maximum value of the sub-Problem of loading the pre-i-1 item into the backpack with a capacity of v-C [I] F [I-1] [v-C [I] (first, add the I-th item to the backpack, and then consider arranging the remaining space capacity) B. if you do not add item I, load the maximum value obtained from the previous item F [I] [v], it must be equal to the maximum value of F [I-1] [v] to the sub-problem loaded into the backpack with a capacity of v. Obviously, the greatest value of the current problem is F [I] [v], which is better than the above two solutions! The Code is as follows:

Import java. util. success;/*** 01 backpack problem * @ author honest **/public class ZeroOnePack {public static void main (String [] args) {int N; // Number of backpacks int [] weight; // weight of a single item int [] value; // value of a single item int [] [] values; // value of int maxValue; // maximum value of memory in = new memory (System. in); maxValue = in. nextInt (); N = in. nextInt (); weight = new int [N + 1]; value = new int [N + 1]; values = new int [N + 1] [maxValue + 1]; for (int I = 1; I <= N; I ++) {weight [I] = in. nextInt (); value [I] = in. nextInt () ;}for (int I = 0; I <= N; I ++) values [I] [0] = 0; for (int I = 0; I <= maxValue; I ++) values [0] [I] = 0; for (int I = 1; I <= N; I ++) {for (int j = 1; j <= maxValue; j ++) {if (weight [I]> j) {values [I] [j] = values [I-1] [j];} else {values [I] [j] = Math. max (values [I-1] [j-weight [I] + value [I], values [I-1] [j]) ;}} // reverse query the selected backpack int j = maxValue; for (int I = N; I> 0; I --) {if (values [I] [j]> values [I-1] [j]) {System. out. print (I + ""); j = j-value [I]; if (j <0) break;} in. close ();}}

 

Test data: Sample Input 10 3 3 3 7 9 9 Sample Output 10 Sample Input 6 5 1 1 3 5 3 10 8 6 5 7 Sample Output 15

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.