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

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 231).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14

解题思路:简单的01背包(dp)。

AC代码一:(二维数组实现)

 #include<bits/stdc++.h>
using namespace std;
const int maxn=;
int t,n,W,v[maxn],w[maxn],dp[maxn][maxn];
int main(){
while(cin>>t){
while(t--){
cin>>n>>W;
for(int i=;i<=n;++i)cin>>v[i];
for(int i=;i<=n;++i)cin>>w[i];
memset(dp,,sizeof(dp));
for(int i=;i<=n;++i){
for(int j=;j<=W;++j){
if(j<w[i])dp[i][j]=dp[i-][j];//无法挑选这个物品
else dp[i][j]=max(dp[i-][j],dp[i-][j-w[i]]+v[i]);//拿和不拿的两种情况都试一下
}
}
cout<<dp[n][W]<<endl;
}
}
return ;
}

AC代码二:(一维数组实现)

 #include<bits/stdc++.h>
using namespace std;
int value[],weight[],dp[];//dp数组始终记录当前体积的最大价值
int main()
{
int T,N,V;
cin>>T;
while(T--){
cin>>N>>V;
for(int i=;i<N;i++)cin>>value[i];//输入价值
for(int i=;i<N;i++)cin>>weight[i];//输入体积
memset(dp,,sizeof(dp));//初始化
for(int i=;i<N;i++){ //个数
for(int j=V;j>=weight[i];j--) //01背包
dp[j]=max(dp[j],dp[j-weight[i]]+value[i]); //比较放入i物体后的价值与不放之前的价值,记录大的值
}
cout<<dp[V]<<endl;//输出总体积的最大价值
}
return ;
}

题解报告:hdu 2602 Bone Collector(01背包)的更多相关文章

  1. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  2. HDU 2602 Bone Collector(01背包裸题)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. HDU 2602 Bone Collector --01背包

    这种01背包的裸题,本来是不想写解题报告的.但是鉴于还没写过背包的解题报告.于是来一发. 这个真的是裸的01背包. 代码: #include <iostream> #include < ...

  4. HDU 2602 Bone Collector (01背包DP)

    题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...

  5. [HDU 2602]Bone Collector ( 0-1背包水题 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...

  6. HDU 2602 Bone Collector (01背包问题)

    原题代号:HDU 2602 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 原题描述: Problem Description Many yea ...

  7. 解题报告:hdu2602 Bone collector 01背包模板

    2017-09-03 15:42:20 writer:pprp 01背包裸题,直接用一维阵列的做法就可以了 /* @theme: 01 背包问题 - 一维阵列 hdu 2602 @writer:ppr ...

  8. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  9. HDU 2602 Bone Collector 0/1背包

    题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  10. hdu 2602 Bone Collector(01背包)模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...

随机推荐

  1. 利用NSA的MS17-010漏洞利用工具实现Win 7和Win Server 2008系统入侵

    影子经纪人(Shadow Brokers)最近陆续曝光的NSA网络武器令人震惊,尽管这些工具是否出自国家级别黑客团队之手尚不清楚,但至少存在一个可以说明问题的事实:这些漏洞利用工具都能有效运行,且具有 ...

  2. Hadoop在window上运行 user=Administrator, access=WRITE, inode="hadoop"

    win7下eclipse中错误的详细描述如下: org.apache.hadoop.security.AccessControlException: org.apache.hadoop.securit ...

  3. JAVA_the user operation is waiting怎么办

    彻底解决 MyEclipse出现the user operation is waiting的问题   2011-05-31 10:32:30|  分类: 软件编程 |  标签:java  myecli ...

  4. Visual Studio VS如何切换代码自动换行

    工具-选项-文本编辑器-自动换行      

  5. C++MFC编程笔记day06 MFC向导、MFC画图类使用

    MFC画图    MFC画图类包含画图设备类和画图对象类    1 画图设备类      CDC类-父类是CObject,封装的是一般的画图设备,比如:显示器,            打印机等.    ...

  6. 【Mongodb教程 第八课 】MongoDB 更新文档

    MongoDB的 update() 和 save() 方法用于更新文档的集合. update()方法更新现有的文档值,而替换现有的文档通过的文件中 save() 方法. MongoDB Update( ...

  7. #Virtual hosts #Include conf/extra/httpd-vhosts.conf 开启就不能启动apache

    #Virtual hosts#Include conf/extra/httpd-vhosts.conf我只要把其中任何一个开启就是吧#去掉就启动不了apache.怎么回事error.log是这样的ht ...

  8. iOS 配置支付宝

    尽管非常easy,可是对于第一次接触支付宝配置的啊猿.有些细节摸不着头脑.今天就来写一个流程配置. 1.创建一个project,然后再创建一个目录,把支付宝sdk要用到的都拖到目录中.然后拖到proj ...

  9. UISlider无法拖动进度条的问题解决

    UISlider无法拖动进度条的问题解决 最近业务中的视频播放使用到了UISlider,但是有一个奇怪的问题,就是在Modar出来的控制器中UISlider是可以正常使用的,但是在Push出来的控制器 ...

  10. VS2010打开高版本VS解决方案

    http://blog.csdn.net/backspace110/article/details/62111273 Microsoft Visual Studio Solution File, Fo ...