HDU 2602 Bone Collector【01背包】

大意:01背包

分析:01背包

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 using namespace std;
 6 
 7 const int maxn = 1005;
 8 
 9 int n, v;
10 int we[maxn], val[maxn], dp[maxn];
11 int solve() {
12     memset(dp, 0, sizeof(dp));
13     for(int i = 1; i <= n; i++) {
14         for(int j = v; j >= we[i]; j--) {
15             dp[j] = max(dp[j], dp[j - we[i]] + val[i]);
16         }
17     }
18     return dp[v];
19 }
20 
21 int main() {
22     int t;
23     scanf("%d",&t);
24         while(t--) {
25         scanf("%d %d",&n, &v);
26         for(int i = 1; i <= n; i++) {
27             scanf("%d", &val[i]);
28         }
29         for(int i = 1; i <= n; i++) {
30             scanf("%d",&we[i]);
31         }
32         printf("%d\n",solve());
33     }
34     return 0;
35 }
View Code

 

posted @ 2014-08-28 21:41  悠悠我心。  阅读(124)  评论(0编辑  收藏  举报