1. Binary optimization:
The idea of binary optimization is still very clever, according to c [i] to get a set of such numbers 2^0,2^1,2^2,2^3.....2^(k-1) , c-2^k+1Where k is the maximum value that satisfies 2^k less than c, like c=7=111, 2^k=100=4;
c=9=1001, 2^k=1000=8 ; c=8=1000 2^k=0100=4
What is the purpose of getting this set of numbers,
All numbers between 1 and c can be obtained from this group of numbers (choose to add), and any number from this group of numbers (each number can only be selected once) must be added together. Is in the closed interval of 1~c
For example, 14=1110 The corresponding set of numbers is: 1, 2, 4, 7 You can try it. Through the first three combinations, you can get all the numbers between 1~7, and add 7 to get 8~14 All the numbers in between, so we can get any number between 1~14, so we can only backpack this group of numbers one by one, instead of backpacking 01 from 1 to c, it is O(c) Partially optimized to O (logc)
void erjinzhi()
{
int i,j,k,m;
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
{
for(k=1;k<<1<c[i];k<<=1)
{
for(j=V;j>=k*v[i];j--)
{
dp[j]=max(dp[j],dp[j-k*v[i]]+k*w[i]);
}
}
m=c[i]-k+1;
for(j=V;j>=m*v[i];j--)
{
dp[j]=max(dp[j],dp[j-m*v[i]]+m*w[i]);
}
}
}
Refer to the ideas of this blog http://blog.csdn.net/flyinghearts/article/details/5898183 and the code of Lord K http://blog.csdn.net/lxy767087094/article/details/54730613, plus myself Understanding
The general idea of that blog is very good. The implementation process uses two queues, one auxiliary queue, which is not easy to understand
struct node
{
int num,value;
}que[100000];
int tail,head;
void push(int x,int y)
{
while(tail>head&&que[tail-1].value<y)
{
tail--;
}
que[tail].num=x;
que[tail++].value=y;
}
void singlequeue()
{
int i,d,j;
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
{
c[i]=min(c[i],V/v[i]);
for(d=0;d<v[i];d++)
{
head=tail=0;
for(j=0;j<=(V-d)/v[i];j++)
{
push(j,dp[j*v[i]+d]-j*w[i]);
while(que[head].num<j-c[i]&&tail>head)
head++;
dp[j*v[i]+d]=que[head].value+j*w[i];
}
}
}
}
Hybrid backpack A hybrid backpack means that some items have only one item, some have countless items, and some are limited items. Then the solution of this kind of backpack is divided into two catego...
Multiple knapsack problem: The number of items w[ i ] is limited and not necessarily the same, and each item has its value v[ i ], seeking the highest value in such cases. Full backpack: choose...
This article refers to: Multiple backpacks: There are n kinds of items and backpacks with a weight of m. Each item has a limited number of num[i], each item has a corresponding weight weight [i] and v...
WeChat public account:Jerry's algorithm and NLP The knapsack problem is a classic dynamic programming model. It is simple and easy to understand, and to a certain extent can reveal the essence ...
Some notes on "Nine Lectures on Backpacks" At the recommendation of a friend, I have read the nine lectures of classic dynamic planning backpack written by dd_engi, and refreshed my understa...
WeChat public account:Jerry's algorithm and NLP The knapsack problem is a classic dynamic programming model. It is simple and easy to understand, and to a certain extent can reveal the ess...
This articleBased on this, it is best to look at it before reading this article. The previous article introduced the 01 backpack problem. Next I will introduce the expansion of the 01 backpack: multip...
Multiple backpack Multiple backpack 1 Original title link problem analysis program Multiple backpack 2 source topic analysis program Hybrid backpack Question source topic analysis program Multiple bac...
Multiple backpack There is a backpack with a total volume of V, there are n items, and the volume is v i v_i vi, The value is w i w_i wi, The quantity is c i c_i ci, How to get the maximum vo...