Dynamic Programming 0-1 knapsack problem

Source: Internet
Author: User

Dynamic Programming 0-1 knapsack problem

? Description: N items and a backpack are given. The Weight of item I is wi, its value is VI, and the size of the backpack is C. How should I select the items to be loaded into the backpack to maximize the total value of the items in the backpack ?? For an item, either it is packed into a backpack or it is not loaded. Therefore, the loading status of an item can be 0 and 1. We set the loading status of item I to XI, XI, (0, 1). This problem is called the 0-11 backpack problem. Process Analysis

Data: number of items n = 5, item weight W [N] = {,}, item value V [N] = },

(Set 0th bits to 0, which is easy to unify with The subscripts. It is not particularly useful and cannot be processed in this way .) Total weight c = 10.

? The maximum size of the backpack is 10, so when the array m size is set, the row and column values can be set to 6 and 11, then, for M (I, j), it indicates that the optional items are I... When the n-pack capacity is J (total weight), the maximum value of the items in the backpack is.




The source code is as follows:

# 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, 6, 5, 4}; // the weight of the item, which is not used at position 0. Const int V [] = {0, 6, 3, 5, 4, 6}; // the corresponding item to be added, position 0 is left blank. 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 indicates the number of items {// The sequence from bottom to top is used to set the value of M [I] [J] // put W [N] For (Int J = 0; j <= C; j ++) if (j <W [N]) m [N] [J] = 0; // J is less than W [N], set the corresponding value to 0. Otherwise, else M [N] [J] = V [N] can be placed. // you can 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]; // assuming 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 the marker is placed or not placed. 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 <= 10; 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 Programming 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.