Answers to knapsack problems-Analysis of poj3624

Source: Internet
Author: User

Exercise Web site: http://poj.org/problem? Id = 3624

Question Analysis: this type of question limits the total quantity and total quality of the item, and initializes the initial quality and a quantified nature of each item. Finally, we can solve the problem of how many values are quantified.

This type of problem can be solved through: the optimal solution of the parent problem depends on the optimal solution of some sub-problems. This is the so-called optimal sub-structure.

Core Idea: dp (I, j) = max {dp (I-1, J), DP (I-1, J-CI) + wi}

AC code analysis:

# include
# include
# include
# include
using namespace STD;
int bag [12900]; // backpack
int W [3410], V [3410]; // two attributes
int main ()
{< br> int n, m;
CIN >>n> m; // initial quantity and Specified Quality
for (INT I = 1; I <= N; I ++) // This is exactly two arrays
CIN> W [I]> V [I]; // all input is completed
// memset (bag, 0, sizeof (BAG); this statement is mainly used to clean up a small number of functions can not be used
for (INT I = 1; I <= N; I ++)
for (int K = m; k> = W [I]; k --)
If (BAG [k-W [I] + V [I]> bag [k]) // process of finding two maximum values
bag [k] = bag [k-W [I] + V [I];
cout return 0;
}

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.