Talking about Packet backpack

Source: Internet
Author: User
Packet backpack, a condition evolved from the base backpack.


The specific question is this. The specific question is this. A backpack with a capacity of V, and several sets of items, each containing a number of items, which are different, and volume W and value p are different. Objects in the group conflict. The maximum value can be obtained in the case of no more than v.


At first glance it seems very difficult to look, in fact, carefully think about it is very simple, this problem can be solved with 01 knapsack. For packet packs, can think so: although divided into many groups, but can only choose one, or not, this and 01 backpack is the same, that is, for each of the 01 backpack unique items, the corresponding packet pack is each group to select an item, so look, is exactly 01 knapsack problem.


Here is the equation of state:
for (int i = 1; I <= z; i++)
for (int j = V; J >= 1; j--)
for (int k = 1; k <= N; k++)
DP[J] = max (Dp[j-w[i][k]] + p[i][k], dp[j]);


where z is the number of packets, V is the backpack volume, n is the number of items per group, W and P are the volume and value of the first group K items.


Why do you write that? Think about the equation of state for the 01 backpack, which is the same as this outer two-layer loop, not the same is added to the inside of the layer, this cycle is to traverse each group of items used, for dp[] Each state is a cycle of each group of items only to the next one, so no effect on the back, It is guaranteed that there is only one item in each group.

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.