Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 44257    Accepted Submission(s): 18442

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
 
01背包模板题
/*
Accepted 2602 78MS 5360K 552 B G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
int dp[MAXN][MAXN];
int n,W;
int v[MAXN],w[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&W);
memset(dp,,sizeof(dp));
for(int i=;i<n;i++) scanf("%d",&v[i]);
for(int i=;i<n;i++) scanf("%d",&w[i]); for(int i=;i<n;i++)
for(int j=;j<=W;j++)
if(j<w[i]) dp[i+][j]=dp[i][j];
else dp[i+][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]); printf("%d\n",dp[n][W]);
} return ;
}

贴一个记忆化搜索

/*
Accepted 2602 327MS 5432K 613 B G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
int dp[MAXN][MAXN];
int n,W;
int v[MAXN],w[MAXN];
int dfs(int i,int wei)
{
if(dp[i][wei]) return dp[i][wei];
if(i==n) return ;
int res;
if(wei>=w[i]) res=max(dfs(i+,wei-w[i])+v[i],dfs(i+,wei));
else res=dfs(i+,wei);
return dp[i][wei]=res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&W);
memset(dp,,sizeof(dp));
for(int i=;i<n;i++) scanf("%d",&v[i]);
for(int i=;i<n;i++) scanf("%d",&w[i]);
printf("%d\n",dfs(,W));
} return ;
}

HDU2602(01背包)的更多相关文章

  1. HDU2602 Bone Collector(01背包)

    HDU2602 Bone Collector 01背包模板题 #include<stdio.h> #include<math.h> #include<string.h&g ...

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

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

  3. HDU2602 Bone Collector 【01背包】

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

  4. hdu2602 Bone Collector (01背包)

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

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

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

  6. 01背包模板题 hdu2602 Bonecollector

    由于数组的滚动过程中当前值(i,j)的更新需要用到上一层的(i-1,j-wi)的值,所以在更新当前的j之前不能更新上一层的j之前的值,故01背包是从后向前更新的(重量取值是从大到小的). 代码如下: ...

  7. hdu-2602&&POJ-3624---01背包裸题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 https://vjudge.net/problem/POJ-3624 都是01背包的裸题 这 ...

  8. HDU 2639(01背包求第K大值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...

  9. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

随机推荐

  1. 微信小程序制作商 业务流程

  2. R语言数据分析系列之五

    R语言数据分析系列之五 -- by comaple.zhang 本节来讨论一下R语言的基本图形展示,先来看一张效果图吧. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...

  3. python(11)- 文件处理

    文件操作 1.1 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下: 昨夜寒蛩不住鸣. 惊回千里梦,已三更. 起来独自绕阶行. 人悄悄,帘外月胧明 ...

  4. python(25)- 面向对象补充Ⅰ

    一.如何使用类 1.实例化:创建对象 类名加括号就是实例化,会自动触发__init__函数的运行,可以用它来为每个实例定制自己的特征. 例子一 x=int(10) print(x) python中一切 ...

  5. shell脚本编写-自动部署及监控

    1.编写脚本自动部署反向代理.web.nfs: I.部署nginx反向代理两个web服务,调度算法使用加权轮询 II.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: ...

  6. 轻松搞定RabbitMQ(四)——发布/订阅

    转自 http://blog.csdn.net/xiaoxian8023/article/details/48729479 翻译地址:http://www.rabbitmq.com/tutorials ...

  7. VMware安装ubuntu学习笔记(只是笔记)

    VMware安装ubuntu开机黑屏/死机 1- Edit Ubuntu VM Configuration file (.vmx) 2- Add the following line cpuid.1. ...

  8. 关于erlang的向上取整和向下取整

    在erlang的API中,erlang:trunc/1 是就近取整,erlang:round/1是四舍五入的, 整理下:对于正数的向上和向下取整, %% 向上取整 ceil(N) -> T = ...

  9. NGUI研究之3D模型坐标转2D屏幕坐标-血条

     刚好今天有朋友问我,比較典型的样例就是游戏里面人物的血条. 原理非常easy就是把3D点换算成2D的点.可是因为NGUI自身是3D所以我们须要先把NGUI下的点转成2D点.然后在把他转成3D的点 ...

  10. vs2005 未能完成操作。未指定的错误

    具体解决过程是这样的: 1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2008右边的“解决方 ...