01 Backpack--An introductory topic in dynamic planning

Source: Internet
Author: User
<strong><span style= "FONT-SIZE:18PX;" > First of all, what is dynamic planning: Often hear   dp:dynamic programming abbreviations Here's the entry question: the                                           01 backpack has a weight and value of n items of WI and VI respectively  . Please select items from which to find the greatest value without the weight of a combination of W. Example: Input:n = 4 (w, v) = {(2.3), (1, 2), (3, 4), (2, 3)}w = 5outtput:7 (items selected 0, 1, 3rd) #include <iostream> #include < ;cstdio> #define MAXN 1000using namespace Std;int N, w;int W[MAXN], v[maxn];int rec (int i, int j) {int res;if (i = = N) {ret Urn res = = 0;} else if (J < W[i]) {res = rec (1 + i, j);} Else{res = max (REC (i + 1, j), REC (i + 1, j-w[i]) + v[i]);} return res;} int main () {cin >> n;for (int i = 0; i < n; i++) {cin >> w[i] >>v[i];}   Cin>>w;cout<<rec (0, W) <<endl;return 0;} </span></strong>

01 Backpack--An introductory topic in dynamic planning

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.