题目链接: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. java nio实现非阻塞Socket通信实例

    服务器 package com.java.xiong.Net17; import java.io.IOException; import java.net.InetSocketAddress; imp ...

  2. 【APUE】线程与信号

    每个线程都有自己的信号屏蔽字,但是信号的处理是进程中所有线程共享的.进程中的信号是递送到单个线程的. 线程中pthread_sigmask函数类似与进程的sigprocmask函数,可以用来阻塞信号. ...

  3. [RxJS] Implement the `map` Operator from Scratch in RxJS

    While it's great to use the RxJS built-in operators, it's also important to realize you now have the ...

  4. BC一周年B

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  5. [wxWidgets]_[0基础]_[不常见但有用的类wxCmdLineParser]

    场景: 1. 有时候须要构造命令行字符串传递給函数调用,比方CreateProcess,假设參数是动态的,那么就得使用类似std::vector<string>加入单个參数,之后拼接为一个 ...

  6. angular $resource 的 get请求 和 post请求

    1.语法: $resource(url,[paramDefaults],[actions],options); 详解: (1)url:一个参数化的url模板 (2)paramDefaults:url参 ...

  7. Linux-github 搭建静态博客

    1.在Github上创建一个新的Repository 到你的github上 https://github.com去create a new repository命名为 github.myblog 2. ...

  8. wsdl2objc定制(一)namespace

    1.问题抛出: 如今还是有非常多人使用 wsdl2objc 来调用webservice,可是有时候会有不开心的事情发生, <soap:Envelope xmlns:soap="http ...

  9. oracle 10g 实例用localhost无法访问的处理

    我在笔记本上安装了一个Oracle10g,安装好了之后,查看E:\oracle\product\10.2.0\db_1\network\ADMIN\tnsnames.ora文件,发现SID对应的IP地 ...

  10. 2016/5/6 thinkphp ①框架 ② 框架项目部署 ③MVC模式 ④控制器访问及路由解析 ⑤开发和生产模式 ⑥控制器和对应方法创建 ⑦视图模板文件创建 ⑧url地址大小写设置 ⑨空操作空控制器 ⑩项目分组

    真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格不一样) 项目稳 ...