Nine Lectures on Backpacks (3)-Multiple Backpack Problems

Nine Lectures on Backpacks (3)-Multiple Backpack Problems

Refer to the famous Nine Lectures on Backpacking, which can be downloaded here , the complete backpacking problem in the previous article

1.1 problem

There are Nkinds of goods and capacity of a Vbackpack, each item has M i M_iMiPiece is available, ithe cost of putting in the first item is C i C_iCi, The value obtained is W i W_iWi. Find out which items to pack into the backpack to maximize the overall value.

1.2 Problem solving ideas

If you have seen the previous complete knapsack problem , the multiple knapsack problem here should be able to kill in seconds. At that time, it was considered to convert the complete knapsack to 01 knapsack problem to solve, and the multiple knapsack is actually a complete knapsack with restrictions, and of course it can also be transformed into 01 backpack problem.

The method is: divide the i-th item into several items in the 01 backpack, and each item has a coefficient. The cost and value of this item are the original cost multiplied by this factor. Let these coefficients be 1, 2, 2 2,..., 2 k − 1, M i − 2 K + 1 1, 2, 2^2, ..., 2^{k-1}, M_i- 2^K+11,2,22,...,2k1,Mi2K+1 , where k is to satisfyM i − 2 k + 1> 0 M_i-2^k + 1>0Mi2k+1>0 the largest integer (and in fact, is to ensure that the coefficient is equal toM i M_iMi). Give a chestnut, M i M_iMiIs 13, then the corresponding k = 3 k = 3k=3 , all coefficients are 1, 2, 4, 6.

1.3 Sample code

I'm too lazy to write ~ You have to create your own data, similar to the 01 backpack problem code

1.4 Actual combat stage

Do a few questions~

Luogu P1776 Treasure Screening

Title description :

Finally, the problem of the millennium was solved. Little FF found the royal treasure room, which was filled with countless valuable treasures.

Now the little FF can make a fortune, quack. But there are too many treasures here, and the small FF collection vehicle seems to be unable to hold so many treasures. It seems that little FF can only abandon part of its treasures in tears.

Little FF sorted out the treasures in the cave and found that each treasure had one or more pieces. He roughly estimated the value of each treasure, and then began the treasure screening work: Little FF has a collection vehicle with a maximum load of W, and there are a total of n kinds of treasures in the cave, and the value of each treasure is vi v_ivi, The weight is wi w_iwi, Each treasure has mi m_imiPieces. Little FF hopes to choose some treasures to install in the collection vehicle without overloading the collection vehicle to maximize their value.

The input and output format and sample skip, please refer to the original question~


In fact, it's very simple, it's multiple backpacks. The same idea as the above is to add a layer of loop on the basis of the 01 backpack problem, and directly upload the code (with comments!)

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int main() {
    
    
	cin.sync_with_stdio(false), cin.tie(NULL);
	int n, wSum, k;
	cin >> n >> wSum;
	int* v = (int*)malloc(n * sizeof(int));	// 价值
	int* w = (int*)malloc(n * sizeof(int));	// 重量
	int* m = (int*)malloc(n * sizeof(int));	// 数量
	int* dp = (int*)malloc((wSum+1) * sizeof(int));

	memset(dp, 0, (wSum + 1) * sizeof(int));
	for (int i = 0; i < n; i++) {
    
    
		cin >> v[i] >> w[i] >> m[i];
	}
	for (int i = 0; i < n; i++) {
    
    
		for (k = 1; k < (m[i]+1)/2; k*=2) {
    
    		// 穷举前面的二进制项系数
			for (int j = wSum; j >= k * w[i]; j--) {
    
    
				dp[j] = max(dp[j], dp[j - k * w[i]] + k * v[i]);
			}
		}
		k = m[i] - k + 1;		// 最后一个系数,保证系数和为m[i]
		for (int j = wSum; j >= k * w[i]; j--) {
    
    
			dp[j] = max(dp[j], dp[j - k * w[i]] + k * v[i]);
		}
	}
	cout << dp[wSum] << endl;
	free(v);
	free(w);
	free(dp);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44338712/article/details/109117552