Backpack Problem Packet Pack! __ Dynamic Planning

Source: Internet
Author: User
Tags pack

I've come to the water blog again.
Packet backpack is a kind of deformation of LC (01) Backpack. The items in the packet pack are divided into groups, and only one item in each group can be picked up and added to the backpack, which is the difference from the 01 backpack.
In the 01 backpack, we use each item as a dynamic planning stage, but in the packet pack we have to use each group as each stage.
In fact, it's very simple, the code is as follows.

for (int i=1;i<=k;i++) for (
int c=v;c>=0;c--) for
(each item J in Group I)
if (C>=w[i])
F[c]=max (f[c) , F[c-w[j]+v[j]]);

Enumerate each group first, then enumerate the volume, and finally enumerate the items in each group.
Remember that the order must not be disorderly.
is not the feeling and LC (01) backpack very similar ...
Here's an example:

This problem is very simple, in fact, is not a packet backpack, just because I am too lazy to find the topic.
For each topic, there are two options, either to write positive solutions or to cheat points. The two items are in one group and only one of them can be selected.
So in this topic, the number of questions equals the number of groups, and the number of enumerated groups is actually enumerating all the topics.
So say and 01 backpack is similar, just take the optimal value of a choice.
Forget it, give me the code.

#include <bits/stdc++.h>
using namespace std;
int n,t;
int w1[1000001],w2[1000001],v1[1000001],v2[1000001];
int f[1000001];
int main ()
{
    cin>>n>>t;
    for (int i=1;i<=n;i++)
    {
        cin>>v1[i]>>w1[i]>>v2[i]>>w2[i];
    }
    for (int i=1;i<=n;i++) for
    (int c=t;c>=min (w1[i],w2[i)); c--)
    {
        if (w1[i]<=c)
        F[c]=max ( F[c],f[c-w1[i]]+v1[i]);
        if (w2[i]<=c)
        F[c]=max (F[c],f[c-w2[i]]+v2[i]);
    }
    cout<<f[t];
    return 0;
}

Hey, water blog success.

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.