Poj 1276 multi-backpack

Source: Internet
Author: User
/* Poj 1276 the meaning of multiple knapsack questions is probably given a currency m to be raised, given the number of currencies owned N [I] and the value V [I], q: How much can you raise is a multi-backpack problem DP [J] indicates the maximum number of valuable items that a backpack with a capacity of J can hold. Generally, the solution to multiple backpacks is to convert them. complete backpack and 01 backpack for Solution Code As follows: */# include <cstdio >#include <algorithm> using namespace STD; # define maxn 100005 # define INF 99999999int need, N; int DP [maxn]; int ans; struct node {int AM, V;} node [15]; void mut_pack (int I) {If (node [I]. am * node [I]. v> = need) // if the total value of an item is greater than required, it is converted into a full backpack problem {// while (1) {} For (Int J = node [I]. v; j <= need; j ++) {DP [J] = max (DP [J-node [I]. v] + node [I]. v, DP [J]); ans = DP [J]> ans? DP [J]: ans;} return;} int amout = node [I]. am; // printf ("% d \ n", amout); For (int K = 1; k <amout; amout-= K, K * = 2) // disassemble the package into 01 for (Int J = need; j> = K * node [I]. v; j --) {DP [J] = max (DP [J-K * node [I]. v] + K * node [I]. v, DP [J]); ans = DP [J]> ans? DP [J]: ans; // printf ("DP [% d]: % d \ n", J, ANS, DP [J]); // while (1) {}} for (Int J = need; j> = amout * node [I]. v; j --) {DP [J] = max (DP [J-amout * node [I]. v] + amout * node [I]. v, DP [J]); ans = DP [J]> ans? DP [J]: ANS ;}} int main () {// freopen ("in.txt", "W", stdout); While (scanf ("% d ", & amp; need, & amp; n )! = EOF) {for (INT I = 1; I <= N; I ++) scanf ("% d", & node [I]. am, & node [I]. v); For (INT I = 1; I <= need; I ++) DP [I] = 0; DP [0] = 0; ans = 0; for (INT I = 1; I <= N; I ++) mut_pack (I); printf ("% d \ n", ANS );}}

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.