题目链接: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. AE的Annotation学习摘记

    http://xg-357.blog.163.com/blog/static/36263124201151763512894/ IFeatureWorkspaceAnno pFWSAnno = (IF ...

  2. Citrix XenServer

    Citrix XenServer xenserver-test cpu特性码:77fafbff-bfebfbff-00000021-2c100800 xe snapshot-list xen还原快照 ...

  3. 【C】字符串,字符和字节(C与指针第9章)

    C语言没有一种显式的数据类型是字符串的. C语言存储字符串:字符串常量(不能改动).字符数组或动态分配的内存(能够改动) *************************************** ...

  4. org.json.JSONException: A JSONObject text must begin with &#39;{&#39; at character 1 of {解决方法

    在使用java读取一个本地的json配置文件的时候,产生了这个异常:org.json.JSONException: A JSONObject text must begin with '{' at c ...

  5. 微信小程序之 Tabbar(底部选项卡)

    1.项目目录 2.在app.json里填写:tab个数范围2-5个 app.json { "pages": [ "pages/index/index", &qu ...

  6. Java基础实例

    打印等腰三角形代码 public class ForForTest{ public static void main(String []args){ for(int x=0;x<5;x++){ ...

  7. AnkhSVN介绍

    AnkhSVN介绍 Posted on 2012-11-15 23:24 ArRan 阅读(3120) 评论(1) 编辑 收藏 AnkhSVN是一款在VS中管理Subversion的插件,您可以在VS ...

  8. Android5.0(lollipop)新特性介绍(一)

    今年6月的Google I/O大会上.Android L的初次见面我相信让会让非常多android粉丝有些小激动和小期待.当然作为开发人员的我来说,激动不言而喻,毕竟这是自08年以来改变最大的一个版本 ...

  9. malloc内存分配

    网上总结到的信息: (1) 静态分派:是在栈上分配,是由用户自己申请,是由操作系统自己释放的 动态分配:是由编译器分配,操作系统没有提供这样的机制,所以自己申请,必须自己删除! (2)你也要明确.栈的 ...

  10. [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

    黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...