Table of contents
Multiple backpacks (range 0-100):
Two-dimensional cost knapsack problem:
Find the number of solutions to the knapsack problem:
01 Backpack:
Traverse from the maximum capacity to the current to prevent duplication
void solve()
{
int n,m,v,w;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>v>>w;
for(int j=m;j>=v;j--)
{
dp[j]=max(dp[j],dp[j-v]+w);
}
}
cout<<dp[m]<<endl;
return ;
}
Complete Backpack:
Traverse from the current capacity to the maximum, which is exactly the opposite of the 01 backpack
void solve()
{
int n,m,v,w;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>v>>w;
for(int j=v;j<=m;j++)
{
dp[j]=max(dp[j],dp[j-v]+w);
}
}
cout<<dp[m]<<endl;
return ;
}
Multiple backpacks (range 0-100):
Applicable when the range is small, all quantities will be converted to 1 and converted into 01 backpack
void solve()
{
int n,m,v[101001],w[101001],ans=1,a,b,c;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a>>b>>c;
for(int j=1;j<=c;j++)
{
v[ans]=a;
w[ans]=b;
ans++;
}
}
for(int i=1;i<=ans;i++)
{
for(int j=m;j>=v[i];j--)
{
dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
}
}
cout<<dp[m]<<endl;
return ;
}
Hybrid backpack:
void solve()
{
int n,m;
cin>>n>>m;
for (int i=1;i<=n;i++)
{
int v,w,s;
cin>>v>>w>>s;
if(s==0)//当完全背包做
{
for(int j=v;j<=m;j++)
dp[j] = max(dp[j],dp[j-v]+w);
}
else //转化为01背包
{
if(s==-1)
s=1;
for(int k=1;k<=s;k<<=1)
{
for(int j=m;j>=v*k;j--)
{
dp[j]=max(dp[j],dp[j-v*k]+w*k);
}
s-=k;
}
if(s)
{
for(int j=m;j>=s*v;j--)
{
dp[j]=max(dp[j],dp[j-v*s]+w*s);
}
}
}
}
cout<<dp[m]<<endl;
return ;
}
Group backpack:
signed main()
{
int n,m,s,w[1010],v[1010];
cin>>n>>m;
while(n--)
{
cin>>s;
for(int i=1;i<=s;i++)
cin>>v[i]>>w[i];
for(int j=m;j>=0;j--)
{
for(int k=1;k<=s;k++)
{
if(j>=v[k])
dp[j]=max(dp[j],dp[j-v[k]]+w[k]);
}
}
}
cout<<dp[m]<<endl;
return 0;
}
Two-dimensional cost knapsack problem:
In addition to volume restrictions, there are also mass restrictions
void solve()
{
int n,v,m;
cin>>n>>v>>m;
for (int i=1;i<=n;i++)
{
int a,b,w;
cin>>a>>b>>w;
for(int j=v;j>=a;j--)
{
for(int k=m;k>=b;k--)
{
dp[j][k]=max(dp[j][k],dp[j-a][k-b]+w);
}
}
}
cout<<dp[v][m]<<endl;
return ;
}
Find the number of solutions to the knapsack problem:
signed main()
{
int n,m,v,w;
cin>>n>>m;
for(int i=0;i<=m;i++)
cnt[i]=1;
for(int i=1;i<=n;i++)
{
cin>>v>>w;
for(int j=m;j>=v;j--)
{
int t=dp[j-v]+w;
if(t>dp[j])
{
dp[j]=t;
cnt[j]=cnt[j-v];
}
else if(t==dp[j])
{
cnt[j]=(cnt[j]+cnt[j-v])%mod;
}
}
}
cout<<cnt[m]<<endl;
return 0;
}
Today’s recommended music: I Guess I’m Not Good Enough Lost in Illusion
Next article: Explanation and special topics of subarrays