HDU 2602 Bone Collector 动态规划01背包

http://acm.hdu.edu.cn/showproblem.php?pid=2602

题意:

  给你N根骨头,分别有它们的价值和体积,他的袋子有规定最多能装多少体积的骨头,需要你求出他收集的骨头的总体积不超过他的袋子的

情况下算出袋子中骨头价值最高的一个解。

 

坑爹:

  今天才刚看 01背包 ,现在也不是太明白吧,这题是水题,只要套下公式就出来了。

 

解法:

  用01背包那个状态转移方程  (资料上复制的伪代码)

for i=1..N
    for v=V..0
        f[v]=max{f[v],f[v-c[i]]+w[i]};
View Code
 1 #include<iostream>
 2 using namespace std;
 3 
 4 const int maxn = 1000 + 10;
 5 
 6 struct node
 7 {
 8     int price;
 9     int v;
10 }; 
11 
12 int main()
13 {
14     int T;
15     cin>>T;
16     while(T--)
17     {
18         int f[maxn];
19         memset(f,0,sizeof(f));
20         int N;
21         int V;
22         cin>>N>>V;
23         struct node bone[maxn];
24         int i;
25         int j;
26         for(i=1; i<=N; i++)
27         {
28             cin>>bone[i].price;
29         }
30         for(i=1; i<=N; i++)
31         {
32             cin>>bone[i].v;
33         }
34 
35         for(i=1; i<=N; i++)
36         {
37             for(j=V; j>=bone[i].v; j--)
38             {
39                 if(f[j]<f[j-bone[i].v]+bone[i].price)
40                 {
41                     f[j]=f[j-bone[i].v]+bone[i].price;
42                 }
43             }
44         }
45 
46         cout<<f[V]<<endl;
47     }
48     return 0;
49 }

 


posted @ 2012-09-05 10:43  pc....  阅读(196)  评论(0)    收藏  举报