Backpack problem - multiple backpacks

Problem Description

There are N items and a backpack with a capacity W. The most N [I] pieces of the i-I item are available, each weight is W [i], and the value is V [I]. Solving which items will be loaded into the backpack to make the weight of these items do not exceed the backpack capacity, and the total value is maximum.

analysis:

Two most basic algorithms:

1. Use a triple cycle to reciprocate:
State transition type is:dp[i][j]=max{dp[i-1][j-k*w[i]]+k*v[i]|0<=k<=n[i]}

The key code is as follows:

void solve()
{
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<=W; j++)
        {
            for(int k=0; k<=a[i] && k*w[i]<=j; k++)
            {
                dp[i+1][j] = max(dp[i][j], dp[i][j-k*w[i]]+k*v[i]); 
            }
        }
    }
    printf("%d\n",dp[N][W]);
}

However, the complexity is O (V * σn [i]), the efficiency is too low.

2. Convert to 01 backpack
replacing the i-I Item into the article in the N [I] piece 01 backpack, which converts the 01 backpack problem of σn [i], and the complexity is still O (V * ΣN [I]).
The code is as follows:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100+10;
const int max_n = 1000000;
int w[maxn], v[maxn], a[maxn];
int w_n[max_n], v_n[max_n];
int dp[max_n];
int N, W;
int k; 
/ * 01 backpack * /
void solve()
{
    for(int i=0; i<k; i++)
    {
        for(int j=W; j>=w_n[i]; j--)
        {
            dp[j] = max(dp[j], dp[j-w_n[i]]+v_n[i]);    
        }   
    }
    printf("%d\n",dp[W]);
}

int main()
{
    scanf("%d%d",&N,&W);
    k=0;
    for(int i=0; i<N; i++)
    {
        scanf("%d%d%d",&w[i],&v[i],&a[i]);
        // convert the first item to N [i] piece 01 item
        for(int j=0; j<a[i]; j++)
        {
            w_n[k] = w[i];
            v_n[k] = v[i];
            k++;
        }
    }
    solve();
    return 0;
}

Optimal algorithm

The first item is divided into a plurality of items, wherein each item has a coefficient, the weight and value of this item is the original weight and value multiplication by this coefficient. Make these coefficients of 1, 2, 4, ..., 2 ^ (k-1), n ​​[i] -2 ^ k + 1, and K is satisfying N [I] -2 ^ k + 1> 0 Integer. For example, if N [i] is 13, the division of this article is 1, 2, 4, 6, respectively, of 1, 2, 4, 6, respectively.
This is divided into O (LOG N [I]) items, converting the original problem to complexity O (V * σlog n [i]) 01 Backpack problem.
- from "Backpack Nine Speaking"

This isBinarylaw.

code show as below:

#include<cstdio>
#include<algorithm>
using namespace std;

const int maxn = 100+10;
int N,W; 
int w[maxn], v[maxn], a[maxn]; 
int dp[maxn];
/ * 01 backpack * /
void zero_pack(int w, int v)
{
    for(int j=W; j>=w; j--)
    {
        dp[j] = max(dp[j], dp[j-w]+v);
    }
}
/ * Complete backpack * /
void comp_pack(int w, int v)
{
    for(int j=w; j<=W; j++)
    {
        dp[j] = max(dp[j], dp[j-w]+v);
    }
}
/ * Multiple backpacks * /
void mult_pack(int w, int v, int a)
{
    // If the number of items is multiplied by weight is greater than or equal to W, then the optimal solution of this item is directly pushed directly with a complete backpack.
    if(w * a >= W)
    {
        comp_pack(w, v);
        return;
    }
    int k = 1;
    while(k < a)
    {
        zero_pack(k*w, k*v);
        a = a - k;
        k = k*2;
    }
    // Remaining another 01 backpack
    zero_pack(a*w, a*v);
}

int main()
{
    scanf("%d%d",&N, &W);
    for(int i=0; i<N; i++)
    {
        scanf("%d%d%d",&w[i],&v[i],&a[i]);  
    } 
    for(int i=0; i<N; i++)
    {
        mult_pack(w[i], v[i], a[i]);
    }
    printf("%d\n",dp[W]);
    return 0;
}

Intelligent Recommendation

[C++ Backpack] The bizarre problem of multiple backpacks

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

[Multiple backpacks] P4095 Eden’s new backpack problem

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

Algorithm backpack problem - multiple backpacks (Python)

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

More Recommendation

Backpack problem (01 backpack, full backpack, multiple backpacks)

First of all, we first understand what is the backpack problem? There are N kinds of items, their weight and value are w [i], cost [i]. Now there is a backpack, the maximum weight that can be carried ...

Algorithm-backpack problem 01 backpack + complete backpack + multiple backpacks

01 backpack: https://biancheng.love/problem/51/index There are n different items, each item has two attributes,weight, value , now give a backpack with a capacity of w, askThe maximum value of items t...

Backpack problem: 0-1 backpack, complete backpack and multiple backpacks

Reference from: https://www.cnblogs.com/fengziwei/p/7750849.html W is the weight of each item in the following code, V is the value of each item. 01 backpack Complete backpack The difference between c...

Backpack problem: 01 backpack, complete backpack, multiple backpacks

First, 01 backpack problem: There are N items and a backpack for V. The capacity of the first item is CI, and the value obtained is Wi. Solve which items will be loaded into the backpack to make the t...

Backpack problem (01 backpack, complete backpack, multiple backpacks)

Article catalog 1,01 backpack problem 2, complete backpack problem 3, multiple backpack issues 1,01 backpack problem Problem Description: The number of gauges n, and the maximum weight V which can be ...

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

Top