# 0-1 backpack Problems

Source: Internet
Author: User

Just explain the recursive formula

M [I] [J] = max (M [I-1] [J], M [I-1] [J-W [I] + V [I])

M [I] [J] indicates the maximum value that can be obtained when the first I object is loaded into a package with the maximum capacity of J,

If this solution does not include an I object, it is equal to M [I-1] [J], that is, the maximum value obtained by locking the first I-1 object into a bag with a capacity of J;

If I object is included, it is equal to M [I-1] [J-W [I] + V [I]. To put the object I, it must be before I, the package must have at least W [I] capacity remaining, that is, the maximum value of the current package is m [I-1] [J-W [I]. After placing object I, the maximum value is added with the value of object I. V [I];

`# Include <iostream> # include <vector> using namespace STD; int max (int A, int B) {return A> B? A: B;} int main () {int W [] = {,}; // weight of each problem int V [] =, 4,6}; // The value of each question int c = 10; // The packet capacity int M   = {0 }; // M [I] [J] indicates the maximum value of for (INT I = 1; I <6; I ++) {for (Int J = 1; j <11; j ++) {If (j> = W [I]) {M [I] [J] = max (M [I-1] [J], M [I-1] [J-W [I] + V [I]); // recursive formula }}for (INT I = 0; I <6; I ++) {for (Int J = 0; j <11; j ++) {cout <m [I] [J] <";}cout <Endl ;}cout <m   <Endl; // put five objects into a pack with a capacity of 10 to obtain the maximum value of the lock getchar (); Return 0 ;}`

Related Keywords:

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.