HDU 2602 Bone Collector
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 30300 accepted submission (s): 12477
Problem description
Many years ago, in Teddy's hometown there was a man who was called "Bone Collector ". this man like to collect varies of bones, such as dog's, cow's, also he went to the grave...
The bone collector had a big bag with a volume of V, and along his trip of collecting there are a lot of bones, obviusly, different bone has different value and different volume, now given the each bone's value along his trip, Can you calculate out the maximum of the total value the bone collector can get?
Inputthe first line contain a integer t, the number of instances.
Followed by T cases, each case three lines, the first line contain two integer N, V, (n <= 1000, v <= 1000) representing the number of bones and the volume of his bag. and the second line contain N integers representing the value of each bone. the third line contain N integers representing the volume of each bone.
Outputone integer per line representing the maximum of the total value (This number will be less than 231 ).
Sample input15 101 2 3 4 55 4 3 2 1 sample output14 (T group test data): Input n, m in each group to indicate the type of the item and the size of the backpack. The next two rows have n numbers in each row. The first row represents the value, and the second row represents the cost. Each item can only be used once! Output the maximum value that a backpack can hold! Code (1 ):
// 0-1 backpack template question # include <stdio. h> # include <string. h> int W [1002], C [1002]; int DP [1002] [1002]; int max (int A, int B) {return A> B? A: B;} int main () {int t; int I, j; scanf ("% d", & T); int n, m; while (t --) {scanf ("% d", & N, & M); for (I = 1; I <= N; I ++) {scanf ("% d ", & W [I]) ;}for (I = 1; I <= N; I ++) {scanf ("% d", & C [I]);} for (I = 0; I <= N; I ++) DP [I] [0] = 0; For (j = 0; j <= m; j ++) DP [0] [J] = 0; for (I = 1; I <= N; I ++) {for (j = 0; j <= m; j ++) {DP [I] [J] = DP [I-1] [J]; If (j> = C [I]) DP [I] [J] = max (DP [I-1] [J], DP [I-1] [J-C [I] + W [I]);} printf ("% d \ n", DP [N] [m]);} return 0 ;}
Code (2) (written in Liu lujia's book ):
// 0-1 backpack template question # include <stdio. h> # include <string. h> int W [1002], C [1002]; int DP [1002] [1002]; int max (int A, int B) {return A> B? A: B;} int main () {int t; int I, j; scanf ("% d", & T); int n, m; while (t --) {scanf ("% d", & N, & M); for (I = 1; I <= N; I ++) {scanf ("% d ", & W [I]) ;}for (I = 1; I <= N; I ++) {scanf ("% d", & C [I]);} for (I = 0; I <= N; I ++) DP [I] [0] = 0; For (j = 0; j <= m; j ++) DP [0] [J] = 0; for (I = 1; I <= N; I ++) {for (j = 0; j <= m; j ++) {DP [I] [J] = (I = 1? 0: DP [I-1] [J]); If (j> = C [I]) DP [I] [J] = max (DP [I] [J], DP [I-1] [J-C [I] + W [I]);} printf ("% d \ n", DP [N] [m]);} return 0 ;}
Summary of 0-1 backpacks