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. 微信移动支付V3开发详细教程服务端采用.net mvc webapi(C#)

    转自:http://www.kwstu.com/ArticleView/netmvc_201511132050268716 最近开发手机app需要实现移动支付功能,由于考虑支付安全将微信支付生成签名写 ...

  2. C# 代理应用 - Cachable

    C# 代理应用 - Cachable 放心,这次不是说设计模式中的代理模式,说的是C#的RealProxy的用法,主要用于:通过给class贴标签,让class做更多的工作,比如判断是否存在缓存,有则 ...

  3. Software Industry Revolution----POJ3898----DP

    题目地址:http://poj.org/problem?id=3898 题目意思: 给你一个模式串,再给你一个原串,要你去匹配 模式串里面的?可对应任意一个字符 *号可对应0个或多个字符 其中a=1, ...

  4. ADS的go to命令

    我们有时候在一个函数右键没有看到“go to”选项,这是因为没有Make,要先Make之后才能使用go to 命令

  5. Android 短信模块分析(四) MMS之短信的发送与接收

     MMS之短信的发送与接收分析: 一.信息发送: com.android.mms.data.WorkingMessage.java 类 send()函数: public void send() { . ...

  6. 用python写爬虫

    Python提供了许多Module,通过这些Module,可以很简单的做一些工作.比如,要获得cloga这个词在百度搜索结果页中的排名结果(排名结果+URL),这就是一个很简单的爬虫需求. 首先,要通 ...

  7. 学习新手给Android新手的一些学习建议

    时间紧张,先记一笔,后续优化与完善. Shamoo做Android开辟已有一年了,对Android开辟也有一点点了解.上面就给Android新手说说我对Android浅面的意识和一点建议吧,知道的大牛 ...

  8. Spring MVC 笔记 —— Spring MVC 文件上传

    文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...

  9. 用C++实现斐波那契数列

    我是一个C++初学者,控制台输出斐波那契数列. 代码如下: //"斐波那契数列"V1.0 //李国良于2017年1月12日编写完成 #include <iostream> ...

  10. Kubernetes(k8s)容器运行时(CRI)

    Kubernetes节点的底层由一个叫做"容器运行时"的软件进行支撑,它负责比如启停容器这样的事情.最广为人知的容器运行时当属Docker,但它不是唯一的.事实上,容器运行时这个领 ...