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
 
题解:
      在容量为m的背包中放入骨头,每个骨头有一定的体积和一定的价值,问如何装能使背包中背的骨头价值最大。典型的01背包问题,套用模板即可。
      我写了两个,分别是一维数组和二维数组,一起发上来。
 
代码1:
#include <bits/stdc++.h>

using namespace std;
int t,n,m,v[],w[],i,j,f[][];
int main()
{
cin>>t;
while(t--){
cin>>n>>m;
for(i=;i<=n;i++) cin>>v[i];
for(i=;i<=n;i++) cin>>w[i];
memset(f,,sizeof(f));
for(i=;i<=n;i++) //装入第i个骨头时,背包容量为j时的最大价值
for(j=;j<=m;j++){
if(j<w[i]){
f[i][j]=f[i-][j];
}
else{
f[i][j]=max(f[i-][j],f[i-][j-w[i]]+v[i]);
}
}
cout<<f[n][m]<<endl;
}
return ;
}
代码2:

#include <bits/stdc++.h>

using namespace std;
int n,m,f[],v[],w[],i,j,t; int main()
{
cin>>t;
while(t--){
cin>>n>>m;
for(i=;i<=n;i++) cin>>v[i];
for(i=;i<=n;i++) cin>>w[i];
memset(f,,sizeof(f));
for(i=;i<=n;i++)
for(j=m;j>=w[i];j--){
if(f[j]<f[j-w[i]]+v[i])
f[j]=f[j-w[i]]+v[i];
}
cout<<f[m]<<endl;
}
return ;
}
 
 
 

hdu2602 Bone Collector 01背包的更多相关文章

  1. [原]hdu2602 Bone Collector (01背包)

    本文出自:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //=================================== ...

  2. hdu2602 Bone Collector (01背包)

    本文来源于:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //================================== ...

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

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

  4. HDU-2602 Bone Collector——01背包

    首先输入一个数字代表有n个样例 接下来的三行 第一行输入n  和  v,代表n块骨头,背包体积容量为v. 第二行输入n块骨头的价值 第三行输入n块骨头的体积 问可获得最大的价值为多少 核心:关键在于d ...

  5. Bone Collector(01背包+记忆化搜索)

    Bone Collector Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

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

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

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

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

  8. HDU 2602 Bone Collector --01背包

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

  9. ACM HDU Bone Collector 01背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 这是做的第一道01背包的题目.题目的大意是有n个物品,体积为v的背包.不断的放入物品,当然物品有 ...

随机推荐

  1. 为什么要用日志框架 Logback 基本使用

    [日志框架]以时间为单位描述应用项目运行状态:用户下线.接口超时.数据库崩溃等等一系列事件 [日志框架能力] 1.定制输出格式 2.定制输出目标 3.携带 Context 比如 HelloWorld. ...

  2. Linux centos6.7网卡配置

    系统安装完成后 以此执行以下命令 相当于自动获取IP地址 setup命令——Network configuration———Device configuration———eth0 依次保存退出 实际上 ...

  3. rabbitMQ学习1:消息队列介绍与rabbitmq安装使用

    1. 什么是消息队列 生活里的消息队列,如同邮局的邮箱, 如果没邮箱的话, 邮件必须找到邮件那个人,递给他,才玩完成,那这个任务会处理的很麻烦,很慢,效率很低 但是如果有了邮箱, 邮件直接丢给邮箱,用 ...

  4. UE4 位置转换相关函数

    get mouse positionget mouse position scaled by dpiget viewport scaleDeproject Scence to WorldLocal T ...

  5. TensorFlow从入门到理解(二):你的第一个神经网络

    运行代码: from __future__ import print_function import tensorflow as tf import numpy as np import matplo ...

  6. Kaldi的nnet2 Component

    FixedAffineComponent:类 LDA-like 的非相关转换,由标准的 weight matrix plus bias 组成(即Wx+b),通过标准的 stochastic gradi ...

  7. 谈谈jQuery中的数据类型检测

    这次是分享jQuery代码中的一些简写技巧,分析jQuery是如何优化代码的,如何用最少的代码来实现jQuery. 在我们工作中也常常会遇到一些数据类型检测,一些方法调用的形式 1 var arr = ...

  8. 【Thymeleaf】Thymeleaf模板对没有结束符的HTML5标签解析出错的解决办法

    解决方案 spring: thymeleaf: mode: LEGACYHTML5 <dependency> <groupId>net.sourceforge.nekohtml ...

  9. 🍓 react16.2新特性 🍓

    react16.2新特性:组件中可以一次性return 多个子元素(子组件)了,也就是说,想return多个子元素,不用在外面包一个父盒子了. 方法一:把要return的元素放在一个空的jsx里面 方 ...

  10. java基础梳理--朝花夕拾(二)

    1.Java语言语法规则和文件格式: 第一个Java程序:/** 第一个Java程序:控制台输出Hello world!*/public class Test{    //访问修饰符 class关键词 ...