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
 

先将原始问题一般化,欲求背包能够获得的总价值,即欲求前j个物体放入容量为m(kg)背包的最大价值f[j]——使用一个数组来存储最大价值,当j取10时,即原始问题了。而前i个物体放入容量为m(kg)的背包,又可以转化成前(i-1)个物体放入背包的问题。

核心代码:

for(i=0;i<n;i++)
            for(j=v;j>=bone[i].volume;j--)
            f[j]=max(f[j],f[j-bone[i].volume]+bone[i].value);

f[j]=max(f[j],f[j-bone[i].volume]+bone[i].value);即为该问题的状态转移方程

当i==0,bone[0].volume==5时经过一个循环

f[10]=1;

f[9]=1;

f[8]=1;

f[7]=1;

f[6]=1;

f[5]=1;

f[4]=0;

f[3]=0;

f[2]=0;

f[1]=0;

f[0]=0;

当i==1,bone[1].volume==4时经过一个循环

f[10]=3;

f[9]=3;

f[8]=2;//因为飞f[4]==0,f[8]==1,而8-bone[1].volume==4,所以f[8]=max(f[8],f[j-bone[i].volume(4)]+bone[i].value(2));下面同理

f[7]=2;

f[6]=2;

f[5]=2;

f[4]=2;

f[3]=0;//3<5也<4,所以前两个骨头都放不下,下面同理,上面也同理

f[2]=0;

f[1]=0;

f[0]=0;

当i==2,bone[2].volume==3时经过一个循环

f[10]=5;

f[9]=5;

f[8]=5;

f[7]=5;

f[6]=3;

f[5]=3;

f[4]=3;

f[3]=3;

f[2]=0;

f[1]=0;

f[0]=0;

当i==3,bone[3].volume==2时经过一个循环

f[10]=9;

f[9]=9;

f[8]=7;

f[7]=7;

f[6]=7;

f[5]=7;

f[4]=4;

f[3]=4;

f[2]=4;;//在此j循环跳出,上面同理,下面也同理,f[j]>=2;

f[1]=0;

f[0]=0;

当i==4,bone[3].volume==1时经过一个循环

f[10]=14;

f[9]=12;

f[8]=12;

f[7]=12;

f[6]=12;

f[5]=9;

f[4]=9;

f[3]=9;

f[2]=5;;//在此j循环跳出,上面同理,下面也同理,f[j]>=2;

f[1]=5;

f[0]=0;

代码实现:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct bone
{
int volume;
int value;
}bone[];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int t,n,v,i,j,f[];
while(cin>>t)
{
while(t--)
{
cin>>n>>v;
for(i=;i<n;i++)
cin>>bone[i].value;
for(i=;i<n;i++)
cin>>bone[i].volume;
memset(f,,sizeof(f));
for(i=;i<n;i++)
for(j=v;j>=bone[i].volume;j--)
f[j]=max(f[j],f[j-bone[i].volume]+bone[i].value);
cout<<f[v]<<endl;
}
}
return ;
}

bone collector hdu 01背包问题的更多相关文章

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

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

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

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

  3. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

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

  4. 题解报告:hdu 2602 Bone Collector(01背包)

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

  5. hdu 2602 - Bone Collector(01背包)解题报告

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

  6. HDU 2639 Bone Collector II(01背包变型)

    此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...

  7. hdu2602 Bone Collector(01背包) 2016-05-24 15:37 57人阅读 评论(0) 收藏

    Bone Collector Problem Description Many years ago , in Teddy's hometown there was a man who was call ...

  8. HDU2602 Bone Collector 【01背包】

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

  9. 背包!背包!HDU 2602 Bone Collector + HDU 1114 Piggy-Bank + HDU 2191 512

    http://acm.hdu.edu.cn/showproblem.php?pid=2602 第一题 01背包问题 http://acm.hdu.edu.cn/showproblem.php?pid= ...

随机推荐

  1. ASP.NET MVC3开发 - CodeFisrt数据库篇之M层验证之调用远程方法(Remote)验证

    本文讲述在作者在使用.net mvc3进行开发的时候用到的两种调用远程验证的方法,第一种方法比较傻瓜,第二种方法方便好用,调用远程验证是个比较常见的验证方法,比如注册用户时的用户名唯一性验证. 作者原 ...

  2. JS获取ckeditor4.x里的值

    项目中有这样一个需求,使用ckeditor可以上传图片,需要在前端验证一下不可上传多于5张图片. 以下是查看源代码所看到的ckeditor里的值 <p>AAAAA</p> &l ...

  3. 北京南天软件java工程师面试题

    此试题为北京南天软件java工程师面试题(部分) 一.单项选择 (1)下列关于构造方法的叙述中,错误的是(C) A.java语言规定构造方法名与类名必须相同 B.java语言规定构造方法没有返回值,但 ...

  4. 前端MVVM框架avalon - 模型转换1

    轻量级前端MVVM框架avalon - 模型转换(一) 接上一章 ViewModel modelFactory工厂是如何加工用户定义的VM? 附源码 洋洋洒洒100多行内部是魔幻般的实现 1: fun ...

  5. Java-抽象类定义构造方法

    abstract class A {  public static final String INFO="hello world";  public String name=&qu ...

  6. Ajax实现xml文件数据插入数据库(二)--- ajax实现与jsp的数据交互。

    在上一篇文章中我们成功得到了重新组织后的数据,接下来需要做的便是将数据插入到数据库中了.在与数据库打交道的过程中有一些方法是普遍的,我们将这些通用方法封装到一个DbUtil类中,以便复用,封装好的Db ...

  7. Ruby设计模式透析之 —— 组合(Composite)

    转载请注明出处:http://blog.csdn.net/sinyu890807/article/details/9153761 此为Java设计模式透析的拷贝版,专门为Ruby爱好者提供的,不熟悉R ...

  8. python绝技 — 扫描蓝牙RFCOMM信道

    RFCOMM协议 RFCOMM通过蓝牙L2CAP协议模拟了RS232串口.这会与另一台设备建立一个蓝牙连接,模拟一条普通的串行线缆,使用户能够用其他设备控制这一台设备,通过蓝牙打电话,发短信,读取手机 ...

  9. 关于 CentOS 自启动(服务、脚本)

    /etc/init.d /etc/rc.local 其实是软连接,所以,实际上看 /etc/rc.d/ 这个文件夹就好了 rc.local 是自启动脚本 正常情况下,用户自定义的服务在 init.d ...

  10. 微信小程序前置课程:flex布局(二)

    原文:http://www.ruanyifeng.com/blog/2015/07/flex-examples.html 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到 ...