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. Oracle修改字段类型和长度

    Oracle修改字段名 alter table 表名 rename column 旧字段名 to 新字段名 Oracle修改字段类型和长度 alter table 表名 modify 字段名 数据类型 ...

  2. asp.net缓存(转载)

    由于工作的需要,最近对.net缓存做了相关了解和学习.做以下分类: 客户端缓存 第三方的缓存 服务器缓存 客户端缓存 客户端缓存主要是指浏览器帮我们缓存一些页面组件包括脚本,样式,图片等等,由于客户端 ...

  3. JAVA实现数据库数据导入/导出到Excel(POI)

    准备工作: 1.导入POI包:POI下载地址http://mirrors.tuna.tsinghua.edu.cn/apache/poi/release/src/(重要) 如下 2.导入Java界面美 ...

  4. c++中的类型擦除

    (原创)c++中的类型擦除 c++11 boost技术交流群:296561497,欢迎大家来交流技术. 关于类型擦除,可能很多人都不清楚,不知道类型擦除是干啥的,为什么需要类型擦除.有必要做个说明,类 ...

  5. .net SoapHeader验证

    .net SoapHeader验证 在工作中经常用到webservice,在.net 开发中经常用到webservice,在java开发经常用到cxf. 今天闲置没事就介绍下 .net webserv ...

  6. Asp.Net统一前后端提示信息方案

    Asp.Net统一前后端提示信息方案   目录 要解决的问题 自定义XML 前端弹框的实现 后端弹框的实现 扩展和整合 示例代码(不完整)及示例截图 示例下载 要解决的问题 减少弹框前后端代码量 增强 ...

  7. Android中ListView下拉刷新的实现

    ListView中的下拉刷新是非常常见的,也是经常使用的,看到有很多同学想要,那我就整理一下,供大家参考.那我就不解释,直接上代码了. 这里需要自己重写一下ListView,重写代码如下: packa ...

  8. 如何调用在$(function(){ //内部函数代码 });

    这个文件主要完成如何调用在jquery内部定义的函数,主要有两种方法 法①: <script type="text/javascript"> $(function() ...

  9. document.referrer之隐藏来源

    document.referrer document.referrer是用来获取跳转链接的来源,正规的解释是:referrer 属性可返回载入当前文档的文档的 URL. 实际中使用在广告相关业务中较多 ...

  10. 当我们在谈论kmeans(3)

        本系列意在长期连载分享,内容上可能也会有所删改: 因此如果转载,请务必保留源地址,非常感谢! 博客园:http://www.cnblogs.com/data-miner/(暂时公式显示有问题) ...