hdu 2602
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2602
题意:骨头收集者在收集骨头。有容量为V的袋子,n个骨头,给出每个骨头的体积和价值。问最大能收集的价值。01背包。
代码:
1 # include <stdio.h> 2 # include <string.h> 3 4 5 int dp[1010] ; 6 int max(int a, int b){return a>b?a:b;} 7 int val[1010], vol[1010] ; 8 9 10 void work() 11 { 12 int n, v, i, j, ans = 0 ; 13 scanf ("%d%d", &n, &v) ; 14 memset (dp, 0, sizeof(dp)) ; 15 for (i = 0 ; i < n ; i++) 16 scanf ("%d", &val[i]) ; 17 for (i = 0 ; i < n ; i++) 18 scanf ("%d", &vol[i]) ; 19 for (i = 0 ; i < n ;i++) 20 for (j = v ; j >= vol[i] ; j--) 21 { 22 dp[j] = max(dp[j], dp[j-vol[i]]+val[i]) ; 23 ans = max(ans, dp[j]) ; 24 } 25 printf ("%d\n", ans) ; 26 } 27 28 29 int main () 30 { 31 int T ; 32 scanf ("%d", &T) ; 33 while (T--) work() ; 34 return 0 ; 35 }