Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2602
The title requirement is as much as possible, so the initialization time is 0.
If the requirement is exactly full, the initialization time except dp[0] = 0, the other must be set to-inf, indicating the illegal situation.
1#include <bits/stdc++.h>2 using namespacestd;3 intT;4 intdp[1010][1010];5 intv[1010], w[1010];6 intMain ()7 {8 //freopen ("In.txt", "R", stdin);9 //freopen ("OUT.txt", "w", stdout);Tenscanf"%d", &T); One while(t--) A { - intN, V; -scanf"%d%d", &n, &V); theMemset (DP,0,sizeof(DP)); - for(inti =1; I <= N; i++) scanf ("%d", &v[i]); - for(inti =1; I <= N; i++) scanf ("%d", &w[i]); - + for(inti =1; I <= N; i++) - { + for(intj =0; J <= V; J + +) A { at if(J >= W[i]) dp[i][j] = max (dp[i-1][j-w[i]]+v[i], dp[i-1][j]); - ElseDP[I][J] = dp[i-1][j]; - } - } -printf"%d\n", Dp[n][v]); - in } - return 0; to}
Scrolling array:
1#include <bits/stdc++.h>2 using namespacestd;3 intT;4 intdp[1010];5 intv[1010], w[1010];6 intMain ()7 {8 //freopen ("In.txt", "R", stdin);9 //freopen ("OUT.txt", "w", stdout);Tenscanf"%d", &T); One while(t--) A { - intN, V; -scanf"%d%d", &n, &V); theMemset (DP,0,sizeof(DP)); - for(inti =1; I <= N; i++) scanf ("%d", &v[i]); - for(inti =1; I <= N; i++) scanf ("%d", &w[i]); - + for(inti =1; I <= N; i++) - { + for(intj = V; J >= W[i]; j--) A { atDP[J] = max (dp[j-w[i]]+V[i], dp[j]); - } - } -printf"%d\n", Dp[v]); - - } in return 0; -}
HDU 2602 Bone Collector 01 Backpack Template