[Knapsack problem] Multiple backpacks and their optimization

tags: Backpack problem  Dynamic programming  algorithm  

Multipack I

Title description

Plain version code (time complexity: O(NVS))

#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;
}

Update while reading

#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;
}

Use rolling array to optimize one dimension

#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;
}

Multiple knapsack problem II (binary optimization, it can be seen that multiple knapsacks are also special 01 knapsacks)

Title description (note that the data range has been expanded to 1000)

Code (time complexity: O(NVlog(S))

#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;
}

Concise version (update while reading, and scrolling array optimization)

#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;
}

Multiple knapsack problem III

Title description (note the data range)

Monotonic queue optimization (time complexity: O(NV))

Code

#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;
}

Intelligent Recommendation

acwing 5. Multiple knapsack problem II (multiple knapsack binary optimization)

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

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...

Multiple backpacks binary optimization

Example blue book p280 https://ac.nowcoder.com/acm/problem/51167 input Output 8 4 Code...

Multiple backpacks and binary optimization

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]...

More Recommendation

[Optimization of multiple backpacks]

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...

Optimization of multiple backpacks

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...

Multiple backpacks - binary optimization

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...

Binary optimization multiple backpacks

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...

Multiple knapsack problem II: binary optimization

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top