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. SQL Server中日志

    再谈SQL Server中日志的的作用 简介 之前我已经写了一个关于SQL Server日志的简单系列文章.本篇文章会进一步挖掘日志背后的一些概念,原理以及作用.如果您没有看过我之前的文章,请参阅: ...

  2. 【C#】调用DOS命令

    public interface IRunConsole { void Run(); } public abstract class RunConsole:IRunConsole { public a ...

  3. iOS UWebView详解

    有时在项目中我们需要嵌入一些web相关的内容,这时你就要用到一个叫UIWebView的东西(UIWebView还可以打开一些文件等,如pdf等),在android和iOS中都有这个东西,使用起来也很方 ...

  4. linux学习心得之目录树开端与/etc(图文)

    linux学习心得之目录树开端与/etc(图文) linux中“一切皆文件”,学习linux一年了,在学习过程中对目录树的一点心得,分享给大家,有不对的地方敬请斧正. 不多说了,先上图: 根目录: / ...

  5. MongoDB的一些用法(转藏)

    MongoDB是目前工作中经常使用到的NoSQL数据库. 本博客只记录相关理论知识和技巧,涉及到实践的部分都会单开Blog来记录实践过程. ------------------------------ ...

  6. 纯Python综合图像处理小工具(3)10种滤镜算法

    <背景>  滤镜处理是图像处理中一种非常常见的方法.比如photoshop中的滤镜效果,除了自带的滤镜,还扩展了很多第三方的滤镜效果插件,可以对图像做丰富多样的变换:很多手机app实现了实 ...

  7. 【驱动】USB驱动·入门

    [驱动]USB驱动·入门 Preface USB是目前最流行的系统总线之一.随着计算机周围硬件的不断扩展,各种设备使用不同的总线接口,导致计算机外部总线种类繁多,管理困难.USB总线正是因此而诞生的. ...

  8. 【C++自我精讲】基础系列一 指针与引用

    [C++自我精讲]基础系列一 指针与引用   一 前言   指针.引用.指针与引用区别. 二 指针   变量:代码中常常通过定义变量来申请并命名存储空间,并通过变量的名字来使用这段存储空间. //变量 ...

  9. Libevent核心原理

    Libevent 是一个事件驱动框架, 不能仅说他是一个网络库. notejs就是采用与libevent类似的libev来做核心驱动的.   Libevent支持三种事件:io事件.信号事件.时间事件 ...

  10. Android中使用开源框架PagerSlidingTabStrip实现导航标题

    此开源框架官网地址:https://github.com/astuetz/PagerSlidingTabStrip 可以理解为配合ViewPager使用的交互式页面指示器控件. 话不多说,先上效果图: ...