01 Backpack algorithm

Source: Internet
Author: User

Turn: 01 knapsack problem

The basic idea of dynamic programming:

A problem is decomposed into a recursive solution of sub-problems, and the intermediate results are saved to avoid repeated computations. It is usually used to find the optimal solution, and the local of the optimal solution is also optimal. The solution process produces multiple decision sequences, and the next step is always dependent on the result of the previous one and the bottom-up solution.

The dynamic programming algorithm can be decomposed into 4 steps after Binate:

1. Describe the structure of an optimal solution, look for sub-problems, and divide the problem.

2. Define the status. A set of values for each variable associated with a child problem is often defined as a state. The value of a state is the solution to this sub-problem (if there are K variables, generally the K-dimensional array is used to store the solution in each state, and the solution can be recorded according to the array. )。

3. Find the state transition equation. The variable value is generally changed from one state to another.

4. Calculate the value of the optimal solution in a "bottom-up" manner.

5. Build the path to the optimal solution from the calculated information. (The optimal solution is a set of solutions where the problem reaches the optimal value)

The step 1~4 is the basis of the dynamic programming solution problem, if the topic only requires the value of the optimal solution, then step 5 can be omitted.

Knapsack problem

01 Backpack: There are n items and a backpack with a weight of M. (Only one piece per item) The weight of article I is w[i], the value is p[i]. The sum of the values is maximized by solving which items are loaded into the backpack.

Full backpack: There are n items and a backpack with a weight of m, each item has unlimited pieces available. The weight of the article I is w[i], the value is p[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack weight and the maximum value.

Multiple backpacks: There are n items and a backpack with a weight of M. Article I items up to n[i] pieces available, each piece of weight is w[i], the value is p[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack weight and the maximum value.

01 knapsack Problem:

This is the most basic knapsack problem, characterized by: only one piece of each item, you can choose to put or not put.

Define the state with sub-question: c[i][v] means the maximum value that the first I item fits into a backpack with a weight of M. The state transfer equation is:

C[i][m]=max{c[i-1][m],c[i-1][m-w[i]]+p[i]}

This equation is very important, and basically all the equations for knapsack-related problems are derived from it. So it is necessary to explain it in detail: "Put the first I item into a backpack of weight m" This sub-problem, if only consider the article I item strategy (put or not put), then can be converted into a only 1 items of the first I-item problem. If I do not put the article I, then the problem is converted to "the first I-1 items into the backpack of the capacity V", the value of c[i-1][m]; If you put the item I, then the problem is converted to "the first I-1 items into the remaining weight of m-w[i] backpack", the maximum value can be obtained is c[ I-1][m-w[i]] plus the value obtained by placing the item in article I p[i].

Code:

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <queue>6#include <cmath>7 #defineCLR (A, B) memset (A,b,sizeof (a))8 #defineINF 0x7f7f7f7f9 #defineM 1050Ten using namespacestd; One intv[ $][ $];//maximum value of the first I items loaded into a backpack with a capacity of J A intMaxintAintb) - { -     if(a>=b) the         returnA; -     Else returnb; - } -  + intKnapsack (intNintW[],intV[],intX[],intC) - { +     inti,j; A      for(i=0; i<=n; i++) atv[i][0]=0; -      for(j=0; j<=c; J + +) -v[0][j]=0; -      for(i=1; i<=n; i++) -          for(j=1; j<=c; J + +) -             if(j<W[i]) inv[i][j]=v[i-1][j]; -             Else toV[i][j]=max (v[i-1][j],v[i-1][j-w[i]]+v[i]); +j=C; -      for(i=n-1; i>=0; i--)   the     { *         if(v[i][j]>v[i-1][J])//since if set up the item is selected.  $         {Panax Notoginsengx[i]=1; -j=j-W[i]; the         } +         Else Ax[i]=0; the     } +printf"the selected item is: \ n"); -      for(i=0; i<n; i++) $printf"%d", X[i]); $printf"\ n"); -     returnv[n-1][c]; -  the } - Wuyi intMain () the { -     intS//the maximum value gained Wu     intw[ the];//the weight of the item -     intv[ the];//the value of the item About     intx[ the];//selection status of items $     intn,i; -     intC//Backpack Maximum Capacity -n=5; -memset (V,0,sizeof(V)); Aprintf"Please enter the maximum capacity of the backpack: \ n"); +scanf"%d",&C); theprintf"number of items entered: \ n"); -scanf"%d",&n); $printf"Please enter the weight of the item separately: \ n"); the      for(i=0; i<n; i++) thescanf"%d",&w[i]); theprintf"Please enter the value of the item separately: \ n"); the      for(i=0; i<n; i++) -scanf"%d",&v[i]); ins=knapsack (n,w,v,x,c); theprintf"The maximum item value is: \ n"); theprintf"%d\n", s); About } the /* the Ten the 5 + 3 4 5) 3 2 - 3 2 6) 4 3 the */
View Code

01 Backpack algorithm

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.