01 knapsack problems (Dynamic Planning)

Source: Internet
Author: User

The dynamic planning method is usually used to solve optimization problems. There are many feasible solutions for such problems, and one of the optimal solutions can be obtained using dynamic planning. You can follow the steps below to design a dynamic planning algorithm (1) portray the structural features of the optimal solution (2) recursively define the value of the optimal solution (3) calculate the value of the optimal solution, A bottom-up method (4) can be used to construct an optimal solution. The 0-1 knapsack problem is a common example of dynamic planning. Its problems are described as follows:

There are a backpack with a capacity of V and n items. Their volume and value are respectively stored in the array v [I] AND w [I]. Now we want to load the items into the package, the total volume of an item cannot exceed the package capacity. To maximize the value of an item in the package, ask how to install the package.

Assume that the I-th item can be placed or not. If the I-th item is placed, the optimal solution is obtained, it is the optimal solution of the sub-problem to get the problem solution of the items in the front I-1. Create a two-dimensional array, f [I] [j]. I indicates the number of items to be placed, and j indicates the size of the backpack. Put the first I items into the backpack, if you do not put the first item, then the problem is converted into the first I-1 items into the remaining capacity for j-w [I] space, the value gained at this time is the value of the previous I-1 plus w [I]. if the I-th item is chosen not to be placed, then the problem is converted into a backpack with the size of j into the pre-i-1 item, the value is f [I-1] [j]. to sum up, it is:

You can obtain the maximum value from the bottom up when several items are taken out of the first item and placed in the backpack. The algorithm program is as follows:

# Include
 
  
# Include
  
   
Using namespace std; int V; // V is the backpack capacity int I; // I is the number of items int main () {int I; cout <"input num and cap" <
   
    
> I> V; int f (int v [], int w []); int v [11]; int w [101]; for (I = 1; I <= I; I ++) {cin> v [I]> w [I];} cout <
    <
     
Y? X: y;} int f (int v [], int w []) {int I, j; int a [4] [11]; for (I = 0; I <= 3; I ++) // initialize for (j = 0; j
     
      

<

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.