Bone Collector
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 21250 accepted submission (s): 8521
Problem descriptionpolicyears 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
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 Input
15 101 2 3 4 55 4 3 2 1
Sample Output
14
It is a simple 01 backpack problem, which is written using two-dimensional arrays and one-dimensional arrays respectively. This question has a very abnormal data, that is, bone with no value and no size. Nima is unscientific!
# include<stdio.h># include<string.h>const int MAXN = 1000+5;int f[MAXN][MAXN];int knapsack(int n, int m){ int i, j, c[MAXN], w[MAXN]; for(i=1; i<=n; i++) scanf("%d", &w[i]);for(i=1; i<=n; i++) scanf("%d", &c[i]); memset(f, 0, sizeof(f)); for(i=1; i<=n; i++) for(j=0; j<=m; j++) { if(c[i]<=j) { if(w[i]+f[i-1][j-c[i]]>f[i-1][j]) f[i][j] = w[i]+f[i-1][j-c[i]]; else f[i][j] = f[i-1][j]; } else f[i][j] = f[i-1][j]; } return (f[n][m]);}int main(void){ int m, n, i, j, t;scanf("%d", &t);while(t--){ scanf("%d%d", &n, &m); printf("%d\n", knapsack(n, m));}return 0;}
# Include <stdio. h> # include <string. h> const int maxn = 1000 + 5; int f [maxn]; void knapsack (int n, int m) {int I, j; int C [maxn], W [maxn]; memset (F, 0, sizeof (f); for (I = 1; I <= N; I ++) scanf ("% d ", & W [I]); for (I = 1; I <= N; I ++) scanf ("% d", & C [I]); for (I = 1; I <= N; I ++) for (j = m; j> = 0; j --) // valuable data without volume may exist {If (J-C [I]> = 0) {If (F [J-C [I] + W [I]> F [J]) f [J] = f [J-C [I] + W [I] ;}} printf ("% d \ n", F [m]);} int main (void) {int t, n, m; scanf ("% d", & T); While (t --) {scanf ("% d ", & N, & M); knapsack (n, m);} return 0 ;}