Backpack problem 3 (multiple backpacks)

The complete backpack mentioned in the previous article refers to the selection of the most value in the case of an unlimited number of items. Now the multi-clip problem is introduced, that is, the number of items w[ i ] is limited and not necessarily the same, and each item has Its value v[ i ], the most value in this case.

The multi-clip problem is characterized by a large amount of data. If you follow the 01 backpack method to open the dp[ m ] [ n ] array, the traversal will time out, so open dp[ maxn ] when creating the array (maxn is the maximum value that the data can reach) ).
Initializes all arrays dp[ ] to 0 and dp[ 0 ] to 1. Using the double loop i to traverse w[ i ] from 1 to n, the inner loop j traverses from v[ i ] as long as dp[ j - v[ i ] ] is true (ie, the value jv[ i ] can Satisfied) and the dp[ j ] value is false (indicating that the value j has not been satisfied) then the value j is possible. Why is it possible? Because it is possible to reach the value j, it depends on whether the number of v[ i ] reaches the upper limit. How to record the number of w[ i ]? Still have to open an array num[ maxn ] that specifically records the number, initialize the array num[ ] to 0 in the first layer of the loop, once dp[ j - v[ i ] ]&&!dp[ j ]&&num[ j - v[ i ] ]<w[ i ] indicates that the value j is satisfiable, then the value of dp[ j ] is set to true, and then num[ j ]=num[ j - v[ i ] ]+1 The number of items representing the value of v[ i ] corresponding to the value j is increased by 1 on the basis of the value of jv[ i ]. This step is especially important! Then, according to the meaning of the question, what can be done.

Special emphasis! Although the multiple backpack is the last one of the backpack problem, its template is the best operation, almost a hundred sets!
template:

    for(int i=1;i<=n;i++)
    {
        memset(num,0,sizeof(num));
        for(int j=v[i];j<=maxn;j++)
        {
            if(dp[j-v[i]&&!dp[j]&&num[j-v[i]]<w[i]) 
            {
                ....... //The specific operation varies depending on the title
                                 Num[j]=num[j-v[i]]+1; //See the number of uses corresponding to num[j]
            }
        }
    }

Example:http://acm.hdu.edu.cn/showproblem.php?pid=2191
Chinese question, did not understand.
Analysis of the idea: a typical multi-clip problem, open a one-dimensional array dp[], the dimension maxn is the maximum value, then double cycle according to the above idea, dp[maxn] corresponds It is the biggest situation that can be bought.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int main()
{
    int T,n,m;
    int v[105],w[105],num[105],dp[105];
    cin>>T;
    while(T--)
    {
        cin>>m>>n;
        for(int i=0;i<n;i++)
            cin>>v[i]>>w[i]>>num[i];
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
            for(int j=0;j<num[i];j++)
                for(int k=m;k>=v[i];k--)
                    dp[k]=max(dp[k],dp[k-v[i]]+w[i]);
        cout<<dp[m]<<endl;
    }
    return 0;
}

Example 2:http://poj.org/problem?id=1742
The meaning of the title: n kinds of coins, each with its quantity w[ i ], how many kinds of value can be produced for a maximum value m to find no more than the maximum value .
Analysis: This question is based on the idea template set, and one set is in the middle.

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int use[100001];
int n,m;
bool dp[100001];
int v[1001],num[1001];
int main() 
{
    while (scanf("%d %d", &n, &m) != EOF) 
    {
        if (n == 0 && m == 0) break;
        for (int i = 0; i < n; ++i)
            scanf("%d", a+i);
        for (int i = 0; i < n; ++i) 
            scanf("%d", num+i);
        int res = 0;
        memset(dp,false,sizeof(dp));
        dp[0] = true;
        for (int i = 0; i < n; ++i) 
        {
            memset(use,0,sizeof(use));
            for (int j = a[i]; j <= m; ++j) 
            {
                if (!dp[j] && dp[j-a[i]] && use[j-a[i]]<num[i])
                {
                    dp[j] = true;
                    use[j] = use[j-a[i]] + 1;
                    ++res;
                }
            }
        }
        printf("%d\n", res);
    }
 return 0;
}

Intelligent Recommendation

Dynamic planning backpack problem - multiple backpacks

Problem Description: There are n items, their weight is wi, their size is vi, their number is ni, now give you a C-size backpack, how to make a backpack The loaded items have the largest sum of weight...

51NOD-1086 backpack problem V2 (multiple backpacks)

1086 Backpack Problem V2  Base time limit: 1 second Space limit: 131072 KB Score: 40Difficulty: Level 4 algorithmic questions Collection Follow There are N kinds of items, the quantity of each it...

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

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

Top