Bone Collector

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

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
 
Author
Teddy
 
Source
 
 
 
代码(一维数组):
 #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背包的更多相关文章

  1. HDU 2602 Bone Collector (简单01背包)

    Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , i ...

  2. HDU 2602 Bone Collector 0/1背包

    题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  4. hdu 2602 Bone Collector(01背包)模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...

  5. HDU 2602 Bone Collector(经典01背包问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/O ...

  6. HDU 2602 Bone Collector

    http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  7. hdu 2602 Bone Collector 背包入门题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目分析:0-1背包  注意dp数组的清空, 二维转化为一维后的公式变化 /*Bone Coll ...

  8. HDU 2602 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 - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  10. 题解报告:hdu 2602 Bone Collector(01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , in Teddy’s ...

随机推荐

  1. 利用插件对某些网页执行javascript代码

    说明 javascript在浏览器地址栏中可以运行,也可以按F12在控制台中运行,还可以写一个插件让javascript针对某些网页执行,可以使用chrome浏览器的Content scripts,C ...

  2. 《Cracking the Coding Interview》——第3章:栈和队列——题目4

    2014-03-18 05:28 题目:你肯定听过汉诺威塔的故事:三个柱子和N个从小到大的盘子.既然每次你只能移动放在顶上的盘子,这不就是栈操作吗?所以,请用三个栈来模拟N级汉诺威塔的玩法.放心,N不 ...

  3. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  4. initialization of 'zf' is skipped by 'case' label原因及解决方法

    原因:switch 的 case 中不能定义变量,不然就会报错.可能是变量的初始化会因为有时候case条件不被执行而跳过. 解决方法: 1:在case中用{}将代码括起来,这样在{}中就能定义变量了; ...

  5. Edu 0空投合约源码

    https://etherscan.io/address/0xa0872ee815b8dd0f6937386fd77134720d953581#code pragma solidity ^0.4.18 ...

  6. 微信小程序--获取form表单初始值提交数据

    <form bindsubmit="formSubmit"> <view class="txt"> <view class=&qu ...

  7. GeoIP2 数据库更新地址

    GeoIP2 数据库更新地址 数据库文件下载网页地址 http://dev.maxmind.com/geoip/geoip2/geolite2/ http://geolite.maxmind.com/ ...

  8. 【SPOJ220】Relevant Phrases of Annihilation (SA)

    成功完成3连T!   嗯没错,三道TLE简直爽到不行,于是滚去看是不是模版出问题了..拿了3份其他P党的模版扔上去,嗯继续TLE...蒟蒻表示无能为力了... 思路像论文里面说的,依旧二分长度然后分组 ...

  9. POJ 2828 Buy Tickets | 线段树的喵用

    题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...

  10. BZOJ2875 [Noi2012]随机数生成器 【矩阵乘法 + 快速乘】

    题目 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me thod)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a, ...