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. 剑指Offer - 九度1348 - 数组中的逆序对

    剑指Offer - 九度1348 - 数组中的逆序对2014-01-30 23:19 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个 ...

  2. 为什么要搞vim

    一. 先得想清楚折腾vim受的这顿折磨值不值.值.零碎记录几点. 迫使我使用vim的原因如下: (1)之前实习的公司的开发机上只有vim,以后工作的公司也只有vim,同部门的同事大都用vim:如果不用 ...

  3. Python zip()函数实现并行迭代

    示例1: for i, j in zip(range(0, 10), range(1, 11)): print(i, j) 输出结果: 0 11 22 33 44 55 66 77 88 99 10 ...

  4. Box布局管理

    创建wx.BoxSizer对象时可以指定布局方向: hbox = wx.BoxSizer(wx.HORIZONTAL) 设置为水平方向 hbox = wx.BoxSizer() 默认就是就是水平方向的 ...

  5. 【PTA】Tree Traversals Again

    题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...

  6. 聊聊、Mybatis Java注解实现

    AbstractAnnotationConfigDispatcherServletInitializer public class MvcInitializer extends AbstractAnn ...

  7. Eureka 向Server中注册服务

    Eureka支持注册与发现服务,本章讲解如何像服务中心注册服务. 在父工程下创建EurekaClient工程(eureka-provider): pom.xml <?xml version=&q ...

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

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

  9. Visual Studio2015 、2017中如何支持MYSQL数据源

    原文:Visual Studio2015 .2017中如何支持MYSQL数据源 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/ght886/arti ...

  10. DateBase -- Rising Temperature

    Question: Given a Weather table, write a SQL query to find all dates' Ids with higher temperature co ...