Dynamic planning 0-1 knapsack problem

Source: Internet
Author: User

Dynamic planning 0-1 knapsack problem

?description of the problem:given n kinds of goods and a backpack. The weight of item I is WI. Its value is VI, the capacity of the backpack is C. Ask how to choose the items loaded into the backpack, so that the loadingIs the total value of the items in the backpack the most?

? for an item, either put it in a backpack or do not install it. Therefore, the loading status of an item can be 0 and 1. We set the load status of item I to xi,xi∈ (0,1), which is referred to as 0-11 knapsack problem. Process Analysis

Data: Item number N=5, item weight w[n]={0,2,2,6,5,4}, item value v[n]={0,6,3. 5,4,6},

(No. 0 place.) Set to 0, not to participate in the calculation, is only convenient with the following subscript for unification, no special use, can also not be handled. ) Total weight c=10.

If the backpack has a maximum capacity of 10, then when setting the array m size, you can set the row and column values to 6 and 11, then for M (i,j) The optional item is the maximum value of the items placed in the backpack when the I...N backpack capacity is J (total weight).

The following is the source code you wrote:

#include <stdio.h> #include <stdlib.h> #include <iostream> #include <queue> #include <             Climits> #include <cstring>using namespace Std;const int c = 10; The capacity of the Backpack const int w[] = {0,2,2,6,5,4};//The weight of the item. No. No. 0 is not used in the position.

Const int v[] = {0,6,3,5,4,6};//item corresponding to add, Position No. No. 0 is empty. const int n = sizeof (W)/sizeof (W[0])-1; n is the number of items int x[n+1];void package0_1 (int m[][11],const int w[],const int v[],const int n)//n represents the number of items {//used in order from bottom to top to set M[i ][J] Value//first put w[n] for (int j = 0; J <= C; j + +) if (J < W[n]) m[n][j] = 0; J less than W[n], the corresponding value is set to 0, otherwise it is able to place else m[n][j] = v[n]; Place the remaining n-1 items. int i; for (i = n-1; I >= 1, i--) for (int j = 0; J <= C; j + +) if (J < w[i]) m[i][ J] = m[i+1][j];//hypothesis J < W[i], the current position cannot be placed, it is equal to the value of the previous position.

Otherwise, it will be larger than the value after placement. Or do not place the value large. Select the larger of them. else m[i][j] = M[i+1][j] > m[i+1][j-w[i]] + v[i]? M[I+1][J]: m[i+1][j-w[i]] + v[i]; }void answer (int m[][11],const int n) {int j = C; int i; for (i = 1; I <= n-1; i++) if (m[i][j] = = M[i+1][j]) x[i] = 0; else {X[i] = 1; j = J-w[i]; } X[n] = M[i][j]? 1:0; }int Main () {int m[6][11]={0}; Package0_1 (M,w,v,n); for (int i = 0; I <= 5; i++) {for (int j = 0; J <=; j + +) printf ("%2d", M[i][j]); cout << Endl; } answer (M,N); cout << "The best answer is:\n"; for (int i = 1; I <= 5; i++) cout << x[i] << ""; System ("pause"); return 0;}


Dynamic planning 0-1 knapsack problem

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.