hdu2602 Bone Collector
http://acm.hdu.edu.cn/showproblem.php?pid=2602
DP,0-1背包
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 1234 5 6 int n, m; 7 int dp[N], v[N], w[N]; 8 9 int main() 10 { 11 int t, i, j; 12 scanf("%d", &t); 13 while(t-- && scanf("%d%d", &n, &m)) 14 { 15 for(i=1; i<=n && scanf("%d", v+i); i++); 16 for(i=1; i<=n && scanf("%d", w+i); i++); 17 memset(dp, 0, sizeof(dp)); 18 for(i=1; i<=n; i++) 19 { 20 for(j=m; j>=w[i]; j--) 21 { 22 if(dp[j-w[i]]+v[i] > dp[j]) 23 { 24 dp[j] = dp[j-w[i]]+v[i]; 25 } 26 } 27 } 28 printf("%d\n", dp[m]); 29 } 30 return 0; 31 }