tags: Backpack problem Dynamic programming algorithm

#include <iostream>
using namespace std;
const int N = 110;
int n,m;
int v[N],w[N],s[N];
int f[N][N];
int main()
{
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> v[i] >> w[i] >> s[i];
for(int i=1;i<=n;i++)
for(int j=0;j<=m;j++)
for(int k=0;k <=s[i] && k * v[i] <=j;k++) // Limit 1: Quantity, Limit 2: Volume
f[i][j] = max(f[i][j],f[i-1][j-v[i] * k] + w[i] * k);
cout<<f[n][m]<<endl;
return 0;
}
#include <iostream>
using namespace std;
const int N = 110;
int n,m;
int f[N][N];
int main()
{
cin >> n >> m;
for(int i=1;i<=n;i++)
{
int v,w,s;
cin >> v >> w >> s;
for(int j=0;j<=m;j++)
for(int k =0;k<=s && k * v <= j;k++)
f[i][j] = max(f[i][j],f[i-1][j - k * v] + k * w);
}
cout<<f[n][m]<<endl;
return 0;
}
#include <iostream>
using namespace std;
const int N = 110;
int n,m;
int v[N],w[N],s[N];
int f[N];
int main()
{
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> v[i] >> w[i] >> s[i];
for(int i=1;i<=n;i++)
for(int j=m;j>=0;j--)
for(int k=0;k<=s[i] && k * v[i] <=j;k++)
f[j] = max(f[j],f[j - k * v[i]] + k * w[i]);
cout<<f[m]<<endl;
return 0;
}

#include <iostream>
using namespace std;
const int N = 12010, M = 2010; // N = 2000 * log(2000)
// Core: Use binary to split s parts into log(s) parts
int n,m;
int v[N], w[N];
int f[M];
int main()
{
cin >> n >> m;
int cnt = 0;
for(int i=1;i<=n;i++)
{
int a,b,s;
cin >> a >> b >> s;
int k = 1;
while(k <= s)
{
cnt ++ ;
v[cnt] = a * k;
w[cnt] = b * k;
s -= k;
k *= 2;
}
if(s > 0) // The rest are put together
{
cnt ++ ;
v[cnt] = a * s;
w[cnt] = b * s;
}
}
n = cnt; // There are a total of cnt groups, do the 01 backpack again
for(int i=1;i<=n;i++)
for(int j = m; j >= v[i]; j --)
f[j] = max(f[j],f[j - v[i]] + w[i]);
cout<<f[m]<<endl;
return 0;
}
#include <iostream>
using namespace std;
const int N = 12010, M = 2010; // N = 2000 * log(2000)
// Core: Use binary to split s parts into log(s) parts
int n,m;
int f[M];
int main()
{
cin >> n >> m;
for(int i=0;i<n;i++)
{
int v,w,s;
cin >> v >> w >> s;
for(int k = 1;k <= s;k *= 2)
{
for(int j = m;j >= k * v; j -- )
f[j] = max(f[j], f[j - k * v] + k * w);
s -= k;
}
if(s)
{
for(int j = m;j >= s * v;j --)
f[j] = max(f[j],f[j-s * v] + s * w);
}
}
cout<<f[m]<<endl;
return 0;
}

#include <iostream>
#include <cstring>
using namespace std;
const int N = 20010;
int n,m;
int f[N],g[N],q[N];
int main()
{
cin >> n >> m;
for(int i=0;i<n;i++)
{
int v,w,s;
cin >> v >> w >> s;
memcpy(g,f,sizeof f);
for(int j =0;j<v;j++)
{
int hh=0,tt=-1;
for(int k =j;k<=m;k+=v)
{
if(hh <= tt && q[hh] < k - s * v) hh ++;
if(hh <= tt) f[k] = max(f[k],g[q[hh]] + (k - q[hh]) / v * w);
while(hh <= tt && g[q[tt]] - (q[tt] - j) / v * w <= g[k] - (k - j) / v * w) tt--;
q[++ tt] = k;
}
}
}
cout<<f[m]<<endl;
return 0;
}
Portal description There are N items and a backpack with a capacity of V. There are at most si items for the i-th item, each volume is vi, and the value is wi. Solve which items are packed into the ba...
Multiple backpacks and optimization Article directory Multiple backpacks and optimization Description of the topic solution 1. Violence~~ Miracle~~ 2. Binary split optimization Core code (split) 3. Mo...
Original address...
Example blue book p280 https://ac.nowcoder.com/acm/problem/51167 input Output 8 4 Code...
Multiple knapsack problem Ordinary multiple backpack split line------------------------------- Binary optimized multiple backpack Tip: Translate to 0/1 backpack problem Replace the i-th item with n[i]...
What's awesome is to use the binary idea to split in this way, which can simplify the complexity of the for loop and achieve the effect of a single for loop. So just remember the template: In this way...
A mixed backpack problem It turned out to be overtime. Then I heard that multiple backpacks can be optimized. (1) Binary optimization Use s[i] to indicate that the item can be fetched s[i] times. It t...
Problem Description There are N items, the volume of the first item isc[i]Valuew[i], The number of each item is limited,n[i] Now there is a backpack for V, please put in a number of items, so that the...
Problem Description There are N items and a capacity of a backpack. The first item has the most Si pieces, each volume is Vi, and the value is Wi. Solving which items will be loaded into a backpack, w...
Question source analysis 1. Disassemble the multiple knapsack problem into 01 knapsack problem If there are 7 items in item a, they can be disassembled into items a1, a2,..., a7 each But the complexit...