tags: c++ Dynamic programming algorithm
There is a backpack with a total volume of V, there are n items, and the volume is
, The value is
, The quantity is
, How to get the maximum volume
There are three solutions to the multiple knapsack problem: 1. Convert to 01 knapsack 2. Binary decomposition 3. Monotonic queue optimization, generally using binary decomposition.
1. Converting to a 01 backpack is similar to the conversion of a full backpack to a 01 backpack. We have talked about it before.
const int n=1010;
int dp[n];
int Multiple_pack(vector<int>&v, vector<int>&w,vector<int> c, int V)
{
dp[0]=0;
for(int i=0;i<v.size();i++)
{
for(int j=V;j>=v[i];j++)
{
for(int k=0;k<=c[i]&&k<=j/v[i],k++)
{
dp[j]=max(dp[j],dp[j-K*v[i]]+k*w[i]);
}
}
}
return dp[V];
}
The time complexity is
, The space complexity is
2, binary decomposition can optimize the time complexity, the optimization is
. The principle of binary decomposition is to use a series of binary numbers or add a number to represent a specified number.
For example, 12 can be represented by (1 2 4 5)
So through a series of binary numbers, the optimization of multiple knapsacks can be realized.
const int n=1010;
int dp[n];
//Because multiple backpacks will call 01 backpack, we first give 01 backpack, the function of complete backpack
void Zero_one_pack(int v_i, int w_i,int V)
{
for(int i=V;i>=v[i];i--)
{
dp[i]=max(dp[i],dp[i-v[i]]+w[i]);
}
}
void Complete_pack(int v_i, int w_i,int V)
{
for(int i=v[i];i<=V;i++)
{
dp[i]=max(dp[i],dp[i-v[i]]+w[i]);
}
}
int Op1_Multiple_pack(vector<int>&v, vector<int>&w,vector<int> c, int V)
{
dp[0]=0;
for(int i=0;i<v.size();i++)
{
//When the number of c[i] is greater than or equal to the maximum number allowed, go directly to the complete backpack
if(V/v[i]<=c[i])
{
Complete_pack(v[i], w[i],V);
continue;
}
//Otherwise, perform binary optimization
for(int j=1;j<=c[i];j*=2)
{
// 01 backpack equivalent to it
c[i]-=j;
Zero_one_pack(j*v[i],j*w[i],V);
}
//There may be a number that is not binary, such as 5 in our example, continue with 01 backpack
Zero_one_pack(c[i]*v[i],c[i]*w[i],V);
}
return dp[V];
}
Time is complicated
There is a backpack with a total volume of V, there are n items, and the volume is
, The value is
, The quantity is divided into three types,
Means only 1 (01 backpack)
Indicates that the number is unlimited (complete backpack)
Means there is
Quantity (multiple backpacks)
The method is to discuss it separately. According to our previous approach, the multiple knapsack problem and 01 knapsack problem are done according to the 01 knapsack solution, and the complete knapsack is done according to the complete knapsack solution.
int v=1010;
int dp[n];
void Zero_one_pack(int v_i, int w_i,int V)
{
for(int i=V;i>=v[i];i--)
{
dp[i]=max(dp[i],dp[i-v[i]]+w[i]);
}
}
void Complete_pack(int v_i, int w_i,int V)
{
for(int i=v[i];i<=V;i++)
{
dp[i]=max(dp[i],dp[i-v[i]]+w[i]);
}
}
int Op1_Multiple_pack(vector<int>&v, vector<int>&w,vector<int> c, int V)
{
for(int i=0;i<v.size(); i++)
{
if(c[i]==0)
{
//Completely backpack
Complete_pack(v[i],w[i],V);
}
else if(c[i]==-1)
{
//01 backpack
Zero_one_pack(v[i],w[i],V);
}
else
{
//The binary decomposition of the multiple knapsack problem is doing 01 knapsack
for(int j=1;j<=c[i];j*=2)
{
c[i]-=j;
Zero_one_pack(j*v[i],j*w[i],V);
}
//It is possible that there is still a number that is not binary, continue with the 01 backpack
Zero_one_pack(c[i]*v[i],c[i]*w[i],V);
}
}
return dp[V];
}
1086 Backpack Problem V2 Base time limit: 1 second Space limit: 131072 KB Score: 40Difficulty: Level 4 algorithmic questions Collection Follow There are N kinds of items, the quantity of each it...
Before that, we talked about 01 backpack and complete backpack. This time, let’s talk about the funny and weird (I don’t think) multiple backpack problem. Multiple knapsack problem Classic...
link. Topic background "Send a letter without an address. There is a distance between such sentiments. Whose song are you playing and what kind of sentiment you have. Can you tell me about it?&qu...
topic There are N items and one capacity is a backpack of V. The first item has the most SI parts, each volume is Vi, and the value is Wi. Solving which items will be loaded into the backpack, which a...
Template inscription ...
Template inscription ...
Full backpack problem submit questions: now has N N NItems, and one capacity is V V VBackpacks, each item has unlimited pieces to use. First i i iThe cost of the item is C i C_{i} CiValue is W i W_{i...
Title link: hdu2844 The meaning: There are different coins with different values, and their number is limited but different. Now require a combination of coins, how many different prices can be combin...
There are N items and a backpack with a capacity of V. There are three categories of items: Items of the first category can only be used once (01 backpack); The second category items can be used unlim...
101 knapsack problem Have N N N And a capacity of items V V V Backpack. Each item can only be usedonce。 The first i i i Volume of items is v i v_i viThe value is w i w_i wi. Which solving the items ...