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.