首页 > 代码库 > hdu 2602(dp)

hdu 2602(dp)

参考页面:

http://www.yuanjiaocheng.net/CSharp/Csharp-keys.html

http://www.yuanjiaocheng.net/CSharp/csharp-interface.html

http://www.yuanjiaocheng.net/CSharp/Csharp-operators.html

http://www.yuanjiaocheng.net/CSharp/Csharp-if-else.html

http://www.yuanjiaocheng.net/CSharp/Csharp-ternary-operator.html

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=2602

 

 

Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 32499    Accepted Submission(s): 13379


Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
 
 

 

Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
 

 

Output
One integer per line representing the maximum of the total value (this number will be less than 2 31).
 

 

Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
 

 

Sample Output
14
 
 
 
题意 :给你一个包的体积,每块骨头的价值和占用的体积,求出可以放入价值最大方案的价值。
分析 :简单的01背包,纯属模板题,也是我做的第一题背包,就直接贴代码了。
 
技术分享
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 //dp[i][j]表示放入第i块骨头并占用j体积的最大价值,
 8 //c[i]表示第i块骨头的体积
 9 //w[i]表示第i块骨头的价值
10 
11 int dp[1111][1111],c[1111],w[1111];
12 int T,N,V;
13 
14 int main ()
15 {
16     int i,j;
17     scanf ("%d",&T);
18     while (T--)
19     {
20         scanf ("%d%d",&N,&V);
21         for (i=1; i<=N; i++)
22         scanf ("%d",&w[i]);
23         for (i=1; i<=N; i++)
24         scanf ("%d",&c[i]);
25         memset(dp, 0, sizeof(dp));
26         for (i=1; i<=N; i++)
27         {
28             for (j=0; j<=V; j++)
29             {
30                 dp[i][j] = dp[i-1][j];   //这里主要考虑j小于c[i]时放不下第i块骨头
31                 if (j >= c[i])
32                 dp[i][j] = max(dp[i-1][j], dp[i-1][j-c[i]]+w[i]);
33             }
34         }
35         printf ("%d\n",dp[N][V]);
36     }
37     return 0;
38 }
View Code

 

hdu 2602(dp)