[Hiho 07] Full backpack

Source: Internet
Author: User

Title Description

State F[i, J] indicates that the selection of the first I species has been decided, the total need does not exceed J;

State transition Equation F[i, j] = Max{f[i, j–need[i]] + val[i], f[i–1, j]};

The status of the result is expressed as f[n, M].

Note the difference between the state equation and the 01 backpack, which reflects whether the item can take one or the other.

The difference that is reflected in the code is that the order of the inner loops is different.

#include <iostream> #include <algorithm>using namespace Std;int f[100005];int main () {int n, m;cin >> N & Gt;> M;int val, need;for (int i = 0; i < n; i++) {cin >> need >> val;for (int j = need; J <= m; j + +) {F[j] = max (F[j], F[j-need] + val);}} cout << f[m] << endl;return 0;}

REF: Backpack question nine talk

[Hiho 07] Full backpack

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.