About several backpacks (C language) and several backpacks (C language)
I will record and summarize several new questions about my backpack. (Refer to blog: http://blog.csdn.net/mu399/article/details/7722810 and http://blog.csdn.net/u013174702/article/details/45741395)
(1) 01 backpack:
01 state transition equation f [I, j] = Max {f [I-1, j-Wi] + Pi, f [I-1, j]} f [I, j] indicates the maximum value that can be obtained by selecting a number of items in the first item and placing them in a backpack with a load-bearing j.
Pi indicates the value of item I. To put this equation bluntly, we can compare the two types of decisions: putting the first item or not putting the second item, and choosing the one with the greatest value. Let's take an example to understand:
Suppose f [I-1, j] indicates that I have a 8-weight backpack, the maximum value of 9 that can be loaded when only items B, c, d, e are optional, now there is a weight Wi of 2 and the value of Pi is 6.
Item a, consider whether to put a carrying 8 backpack to maximize its value, f [I-1, j-Wi] represents the maximum value of a backpack with a load of 6 (equal to the current backpack with a load of 8 Minus the Weight of item a 2 ),
When only item B, c, d, and e are available, the maximum value of this backpack is 8.
Because f [I-1] [v-Wi] + w [I] = 9 + 6 = 15 greater than f [I] [v] = 8, therefore, item a should be placed in a carrying 8 backpack.
In general:
In the equation, what needs to be placed now is the I-th item, the weight of this item is Wi, the value is Pi, so f [I-1, j] represents not putting this item into a backpack, while f [I-1], j-Wi] + Pi represents the total value of the I-pieces after the backpack, compare the values of the two to obtain the maximum value stored in the backpack.
I have attached a question (49) on Nanyang oj ):
Happy James
Time Limit: 1000 MS | memory limit: 65535 KB
Difficulty: 4
-
Description
-
James was very happy today. The new house purchased at home had the key. There was a very spacious room dedicated to him in the new house. Even more pleased, his mother said to him yesterday: "You have the final say about the items you need to purchase and how to arrange in your room, as long as the price does not exceed N yuan ". James started to make a budget early this morning, but he wants to buy too many things and will definitely exceed his mother's limit of N yuan. Therefore, he defined an importance for each item and divided it into five equal values: an integer of 1 ~ 5 indicates that 5th is the most important. He also found the price of each item on the Internet (all in integer yuan ). He hopes that the sum of the product of the price and importance of each item will be maximized without exceeding N yuan (which can be equal to N yuan. Set the price of item j to v [j], and the importance to w [j]. k items are selected and numbered as j1... jk, then the sum is: v [j1] * w [j1] + .. + v [jk] * w [jk] Please help Jin Ming design a shopping order meeting the requirements.
-
Input
-
Enter an integer N (0 <N <= 101) in the first row to indicate the number of test data groups.
The 1st rows of test data input in each group are two positive integers separated by a space:
N m
(N (<30000) indicates the total amount of money, and m (<25) indicates the number of items to be purchased .)
From row 2nd to row m + 1, row j gives the number of J-1
The basic data of an item. Each row has two non-negative integers.
V p
(V indicates the price of the item (v ≤ 10000), and p indicates the importance of the item (1 ~ 5 ))
-
Output
-
Each group of test data outputs only one positive integer, which is the sum of the product price and importance of an item that does not exceed the total amount of money
Maximum value (<100000000)
-
Sample Input
-
11000 5800 2400 5300 5400 3200 2
-
Sample output
-
3900
Solution code:
// 01 backpack problem, happy James
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;
Int dp [30001]; // dp [I] indicates the maximum value when the quality is I.
Struct bag {
Int v;
Int p;
} A [26];
Int main (){
Int n;
Scanf ("% d", & n );
While (n --){
Int N, m, I, j;
Scanf ("% d", & N, & m );
For (I = 1; I <= m; I ++)
Scanf ("% d", & a [I]. v, & a [I]. p );
Memset (dp, 0, sizeof (dp ));
For (I = 1; I <= m; I ++ ){
For (j = N; j> = a [I]. v; j --){
Dp [j] = max (dp [j-a [I]. v] + a [I]. v * a [I]. p, dp [j]);
} // Determine whether to buy the I-th item with the price of j, always maximizing the value of dp
}
Printf ("% d \ n", dp [N]);
}
}
(2) See 01 backpack again:
See also 01 backpack (through Nanyang Oj an example to reflect) Time Limit: 1000 MS | memory limit: 65535 KB difficulty: 3
-
Description
-
There are n items whose weight and value are wi and vi respectively. select items whose total weight does not exceed W from these items and find the maximum value of the total value of all items in the selection scheme. 1 <= n <= 100 1 <= wi <= 10 ^ 7 1 <= vi <= 100 1 <= W <= 10 ^ 9
-
Input
-
Multiple groups of test data.
Input n and W in the first line of each group of test data, followed by n rows. Input two numbers in each row, indicating the wi and vi of the I item.
-
Output
-
To satisfy the maximum value of the question, each group of test data occupies one row.
-
Sample Input
-
4 52 31 23 42 2
-
Sample output
-
7
At first, I thought this was an ordinary backpack problem. I should take a closer look at the maximum value if dp [W] is used to indicate that the quality is wi, because W's range is too wide to be so large an array,
Therefore, the solution is to flip the value and weight, and use a smaller value to open an array. The final result is to specify the minimum weight of the value.
Code attached:
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;
Struct bag {
Int w;
Int v;
} A [101];
Int dp [100000];
Int main (){
Int n, W;
While (scanf ("% d", & n, & W )! = EOF ){
Int I, j, sum = 0;
For (I = 0; I <n; I ++ ){
Scanf ("% d", & a [I]. w, & a [I]. v );
Sum + = a [I]. v;
}
For (I = 0; I <= sum; I ++)
Dp [I] = 1e9;
Dp [0] = 0;
For (I = 0; I <n; I ++ ){
For (j = sum; j> = a [I]. v; j --){
Dp [j] = min (dp [j-a [I]. v] + a [I]. w, dp [j]); // dp [] indicates the minimum weight of a specified value, and j indicates the specified value.
}
}
For (I = sum; I> = 0; I --) {// output the value of dp in order, that is, the value of weight.
If (dp [I] <= W ){
Printf ("% d \ n", I );
Break;
}
}
}
}
(3) Complete backpack:
Full backpack (Nanyang oj311) Time Limit: 3000 MS | memory limit: 65535 KB difficulty: 4
-
Description
-
A full backpack defines N items and a backpack with a capacity of V. Each item has an unlimited number of items available. The volume of item I is c and the value is w. Solving which items are loaded into a backpack can make the total size of these items not exceed the total size of the backpack, and the total value is the largest. This question requires that when a backpack is filled with a backpack, find the sum of the maximum value. If it cannot be filled with a backpack, output NO
-
Input
-
Row 1: N indicates the number of groups of test data (N <7 ).
Next, the first row of test data in each group contains two integers, M and V. M indicates the number of item types, and V indicates the total size of the backpack. (0 <M <= 2000,0 <V <= 50000)
Each row in the next M row has two integers c, w representing the weight and value of each item respectively (0 <c <100000, 0 <w <)
-
Output
-
Corresponding to the output results of each group of test data (if it can be filled with a backpack, the maximum value of the items in the backpack is output when it is filled with a backpack. If it cannot be filled with a backpack, output NO)
-
Sample Input
-
21 52 22 52 25 1
Code:
# Include <stdio. h>
# Include <algorithm>
Using namespace std;
Int dp [50005], c [2001], w [2001];
Int main (){
Int N, M, V, I, j;
Scanf ("% d", & N );
While (N --){
Scanf ("% d", & M, & V );
For (I = 1; I <= V; I ++)
Dp [I] =-1000000;
Dp [0] = 0;
For (I = 0; I <M; I ++)
Scanf ("% d", & c [I], & w [I]);
For (I = 0; I <M; I ++ ){
For (j = c [I]; j <= V; j ++ ){
Dp [j] = max (dp [j-c [I] + w [I], dp [j]);
}
}
If (dp [V] <0)
Printf ("NO \ n ");
Else
Printf ("% d \ n", dp [V]);
}
}
Solution:
The state transition equation of the 0-1 backpack is:
ForI= 1NForV=VToCIF[V] = Max{F[V], F[V−CI] +WI}
A full backpack can be used without limits on the number of items, that is, an object can be placed repeatedly.
Transfer Equation
ForI= 1NForV=CIToVF[V] = Max (F[V], F[V−CI] +WI)
You will find that this pseudocode has onlyVThe order of loops is different. Why is this algorithm feasible? First of all, let's think about why we need to followVIn descending order. LetVDecrease is to ensureIStatus in the next loopF[I, v] By statusF[I−1, V−CI. In other words, this is to ensure that each item is selected only once, because the quality is reduced and it is impossible to add an item of the same quality as the original one,
But now the characteristics of the full backpack is that each item can choose unlimited items, so the "quality increase" cycle, so the following may continue to add the same quality items..