0-1 knapsack problem (Quick Start to Dynamic Planning)

Source: Internet
Author: User

9. Questions about backpacks

There are n items and a backpack with a capacity of v. The cost of the I-th item is C [I], and the value is W [I]. Solving which items are loaded into a backpack can maximize the total value.

Basic Ideas

This is the most basic problem with a backpack. It features that each item has only one item, and you can choose to put it or not.

Define the state with a subproblem: that is, F [I] [v] indicates the maximum value that a backpack with a capacity of V can obtain when the first I item is placed. The state transition equation is:

f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}

This equation is very important. Basically all the equations related to the backpack are derived from it. Therefore, it is necessary to explain in detail: "Put the first I items into a backpack with a capacity of V, if you only consider the I-th item Policy (put or not put), then it can be converted into a problem that only involves the previous I-1 items. If I items are not put, then the problem is converted to "pre-i-1 items into the capacity of V backpack", the value is f [I-1] [v]; if I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack with the remaining capacity V-C [I ", the greatest value that can be obtained at this time is f [I-1] [V-C [I] plus the value W [I] obtained by placing the I item.

The source code to paste below.

Int V [200] [200]; // The maximum value int max (int A, int B) obtained by loading the first I item into a backpack with a capacity of J) {if (a> = B) return a; else return B;} int knapsack (INT N, int W [], int V [], int X [], int C) {int I, j; for (I = 0; I <= N; I ++) V [I] [0] = 0; For (j = 0; j <= C; j ++) V [0] [J] = 0; for (I = 0; I <= n-1; I ++) for (j = 0; j <= C; j ++) if (j <W [I]) V [I] [J] = V [I-1] [J]; else V [I] [J] = max (V [I-1] [J], V [I-1] [J-W [I] + V [I]); j = C; for (I = n-1; I> = 0; I --) {If (V [I] [J]> V [I-1] [J]) {x [I] = 1; j = J-W [I];} else X [I] = 0;} printf ("the selected item is \ n "); for (I = 0; I <n; I ++) printf ("% d", X [I]); printf ("\ n "); return V [n-1] [c];} void main () {int s; // obtain the maximum value of int W [15]; // item weight int V [15]; // item value int X [15]; // item selection status int N, I; int C; // maximum backpack capacity n = 5; printf ("Enter the maximum backpack capacity: \ n"); scanf ("% d", & C ); printf ("input item count: \ n"); scanf ("% d", & N); printf ("Enter the item weight separately: \ n "); for (I = 0; I <n; I ++) scanf ("% d", & W [I]); printf ("Enter the value of the item separately: \ n "); for (I = 0; I <n; I ++) scanf (" % d ", & V [I]); s = knapsack (n, w, V, X, c); printf ("Maximum item value: \ n"); printf ("% d \ n", S );}

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.