After reading the multiple backpacks mentioned in section 9, this question should be easily made.
Model:
YesN items and a backpack with a capacity of v. A maximum of N [I] items are available for the I-th item. The cost per item is C [I] and the value is W [I]. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.
Method:
Basic Algorithm
This is similar to a full backpack problem. The basic equation only needs to slightly change the equation of the complete backpack problem, becauseThere are N [I] + 1 policies for I items: 0 items, 1 item ...... N [I] parts. If f [I] [v] indicates that the first I items are placed in the maximum weight of a backpack with a capacity of V, there is a state transition equation:
F [I] [v] = max {f [I-1] [V-K * C [I] + K * W [I] | 0 <= k <= N [i]}
Complexity isO (V * Σ N [I]).
Convert 01 backpack Problems
Another basic way to write well is to convert01 backpack solution: Replace the I-th item with the item in the N [I] item 01 backpack, and the 01 backpack problem with the number of items is calculated as Σ N [I, the complexity is still O (V * Σ N [I]).
But we want to convert it01 the complexity of a backpack can be reduced just like that of a full backpack. We still consider the binary idea. We want to replace the I-th item with several items, so that in the original question, the I-th item can be used in every strategy -- 0 .. N [I] parts-can be equivalent to several replacement items. In addition, a policy that exceeds N [I] parts cannot appear.
The method is:An item is divided into several items, each of which has a coefficient. The cost and value of this item are the original cost and value multiplied by this coefficient. Make these coefficients respectively 1, 2, 4 ,..., 2 ^ (k-1), N [I]-2 ^ k + 1, and K is the maximum integer that satisfies N [I]-2 ^ k + 1> 0. For example, if n [I] is 13, the item is divided into four items with a coefficient of 1, 2, 4, and 6.
The coefficient of the divided items isN [I] indicates that the I-th item cannot be obtained more than N [I] pieces. In addition, this method can ensure that 0 .. each integer between N [I] can be expressed by the sum of several coefficients. This proof can be divided into 0 .. 2 ^ K-1 and 2 ^ K .. N [I] is not difficult to discuss separately. I hope you can think about it yourself.
In this wayI items are divided into O (log n [I]) items, which converts the original problem to <math> O (V * Σ log n [I]) complexity. the 0th backpack problem is a great improvement.
The above content comes from the problem of multiple backpacks mentioned in backpack 9.
# Include <iostream> # include <algorithm> # define maxn 100010 using namespace STD; int DP [maxn], n, k; int V [101], W [101], v; void zero (INT cost) {for (INT I = V; I >= cost; I --) DP [I] = max (DP [I], DP [I-cost] + cost);} void complet (INT cost) {for (INT I = cost; I <= V; I ++) DP [I] = max (DP [I], DP [I-cost] + cost);} void multi (INT cost, int amount) {If (Cost * Amount> = V) {complet (cost); return;} int K = 1; while (k <amount) {zero (K * cost ); amount-= K; k <= 1;} zero (Amount * cost);} int main () {While (scanf ("% d", & N, & V) = 2 & (N | V) {for (INT I = 0; I <n; I ++) scanf ("% d ", & V [I]); For (INT I = 0; I <n; I ++) scanf ("% d", & W [I]); for (INT I = 1; I <= V; I ++) DP [I] = int_min; DP [0] = 0; For (INT I = 0; I <n; I ++) Multi (V [I], W [I]); int COUNT = 0; For (INT I = 1; I <= V; I ++) if (DP [I]> = 0) Count ++; printf ("% d \ n", count) ;}return 0 ;}