HDU 2602.Bone Collector-动态规划0-1背包
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 85530 Accepted Submission(s): 35381
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 ?

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.
5 10
1 2 3 4 5
5 4 3 2 1
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int val[N],wei[N],dp[N];
int main(){
int t,n,m;
scanf("%d",&t);
while(t--){
memset(val,,sizeof(val));
memset(wei,,sizeof(wei));
memset(dp,,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%d",&val[i]);
for(int i=;i<n;i++)
scanf("%d",&wei[i]);
for(int i=;i<n;i++){
for(int j=m;j>=wei[i];j--){
dp[j]=max(dp[j],dp[j-wei[i]]+val[i]);
}
}
printf("%d\n",dp[m]);
}
return ;
}
代码(二维数组):
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+;
int val[N],wei[N],dp[N][N];
int main(){
int t,n,m;
scanf("%d",&t);
while(t--){
memset(dp,,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&val[i]);
for(int i=;i<=n;i++)
scanf("%d",&wei[i]);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(wei[i]<=j)dp[i][j]=max(dp[i-][j],dp[i-][j-wei[i]]+val[i]);
else dp[i][j]=dp[i-][j];
}
}
printf("%d\n",dp[n][m]);
}
return ;
}
HDU 2602.Bone Collector-动态规划0-1背包的更多相关文章
- HDU 2602 Bone Collector (简单01背包)
Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , i ...
- HDU 2602 Bone Collector 0/1背包
题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- hdu 2602 Bone Collector(01背包)模板
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 2602 Bone Collector(经典01背包问题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/O ...
- HDU 2602 Bone Collector
http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 2602 Bone Collector 背包入门题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目分析:0-1背包 注意dp数组的清空, 二维转化为一维后的公式变化 /*Bone Coll ...
- HDU 2602 Bone Collector(01背包裸题)
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- 题解报告:hdu 2602 Bone Collector(01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , in Teddy’s ...
随机推荐
- how to export chrome speed dial extension?
locate chrome-extension_dgpdioedihjhncjafcpgbbjdpbbkikmi_0.localstorage, copy it to you want, everyt ...
- b树的实现
花了蛮长时间实现的b树插入操作.有时间再实现其他操作. #include <stdio.h> #include <stdlib.h> #define M 5 enum KeyS ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目5
2014-04-23 18:42 题目:设计一个在线阅读系统的数据结构. 解法:这题目太大了,我的个亲娘.显然你不可能一次加载一整本书,做到单页纸加载的粒度是很必要的.为了读书的连贯效果,预取个几页也 ...
- DOS程序员手册(十一)
560页 版本5中新增加的子功能05h支持程序截获MS-DOS EXEC调用,并实 现自我装载.该子功能能实现内存的修补,如设置装载程序能接收的版本号 (通过SETVER设置的版本号)以及实现对装载程 ...
- es6实现简单模板编译
现在有各种框架,其中一个主要模块就是关于template.最火的vue.react等框架,在这一块上也是是下足了功夫.我也想写一个自己的模板编译工具,所以就做了个简单的实现,主要是使用es6的反引号编 ...
- python中字符串是特殊的列表
for x in range(20): print 'fizz'[x%3*4::]+'buzz'[x%5*4::]or x 这个是由 Jeff Atwood推广的一个编程练习叫FizzBuzz,问题如 ...
- Java 实现二叉树的构建以及3种遍历方法
转载自http://ocaicai.iteye.com/blog/1047397 大二下学期学习数据结构的时候用C介绍过二叉树,但是当时热衷于java就没有怎么鸟二叉树,但是对二叉树的构建及遍历一直耿 ...
- SQL视频总结
SQL是英文Structured Query Language的缩写,意思为结构化查询语言. SQL语言的主要功能就是同各种数据库建立联系,进行沟通.SQL被作为关系型数据库管理系统的标准语言. SQ ...
- 整合S2SH框架
S2SH框架(Struts2,Spring,Hibernate)整合 Struts2.Hibernate和Spring.其中在Struts2部分主要为MVC设计思想,Struts2的处理流程及配置,S ...
- $this和self、parent这三个关键词分别代表什么?在哪些场合下使用?
$this:当前对象 self: 当前类 parent: 当前类的父类 $this在当前类中使用,使用->调用属性和方法. self也在当前类中使用,不过需要使用::调用. 静态属性,不能在类里 ...