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
 
现附上AC代码:

#include<iostream>
#include<cstring>
using namespace std;
const int v=1000+10;
const int num=1000+10;
int value[num][2]={0};
int dp[num][v]={0};
void solve(int s,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<=s;j++) //这道题之前一直wrong answer,找了很久都没发现问题,最后在此处找到了根源,原先写的是int j=1;后来改为0就对了,体积竟然可以是0,我也是无语了
{
if(j>=value[i][0])
dp[i+1][j]=max(dp[i][j],dp[i][j-value[i][0]]+value[i][1]);
else
dp[i+1][j]=dp[i][j];
}
}
}

int main()
{
int n,s,t;
cin>>t;
while(t--)
{
cin>>n>>s;
memset(dp,0,sizeof(dp));
memset(value,0,sizeof(value));
for(int i=0;i<n;i++)
cin>>value[i][1];
for(int i=0;i<n;i++)
cin>>value[i][0];
solve(s,n);
cout<<dp[n][s]<<endl;
}
return 0;
}

找到递推关系即可。分情况:1)若可重复使用物品,则是dp[i+1][j]=max(dp[i][j],dp[i+1][j-value[i][0]]+value[i][1])

2)若不可重复使用,则为dp[i+1][j]=max(dp[i][j],dp[i][j-value[i][0]]+value[i][1])

区别是从本行找还是从上一行找

hdu2602Bone Collector ——动态规划(0/1背包问题)的更多相关文章

  1. 【动态规划】简单背包问题II

    问题 B: [动态规划]简单背包问题II 时间限制: 1 Sec  内存限制: 64 MB提交: 21  解决: 14[提交][状态][讨论版] 题目描述 张琪曼:“为什么背包一定要完全装满呢?尽可能 ...

  2. 经典递归问题:0,1背包问题 kmp 用遗传算法来解背包问题,hash表,位图法搜索,最长公共子序列

    0,1背包问题:我写笔记风格就是想到哪里写哪里,有很多是旧的也没删除,代码内部可能有很多重复的东西,但是保证能运行出最后效果 '''学点高大上的遗传算法''' '''首先是Np问题的定义: npc:多 ...

  3. 蓝桥杯 0/1背包问题 (java)

      今天第一次接触了0/1背包问题,总结一下,方便以后修改.不对的地方还请大家不啬赐教! 上一个蓝桥杯的例题: 数据规模和约定 代码: import java.util.Scanner; public ...

  4. Java实现动态规划法求解0/1背包问题

    摘要: 使用动态规划法求解0/1背包问题. 难度: 初级 0/1背包问题的动态规划法求解,前人之述备矣,这里所做的工作,不过是自己根据理解实现了一遍,主要目的还是锻炼思维和编程能力,同时,也是为了增进 ...

  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-2542-0-1背包问题:饭卡

    解题心得: 这题就是一个简单的0-1背包问题,只不过加了一系列的限制.可以想办法消去限制,直接转换成0-1背包问题的模板形式. 需要注意的几个点:首先对于剩余的5元钱的处理可以直接在总的钱数上将5减去 ...

  7. 动态规划:HDU-1203-0-1背包问题:I NEED A OFFER!

    解题心得: 动态规划就是找到状态转移方程式,但是就本题0-1背包问题来说转移方程式很简单,几乎看模板就行了. 在本题来说WA了很多次,很郁闷,因为我记录v[i]的时候i是从0开始的,一些特殊数据就很尴 ...

  8. 动态规划专题 01背包问题详解 HDU 2546 饭卡

    我以此题为例,详细分析01背包问题,希望该题能够为大家对01背包问题的理解有所帮助,对这篇博文有什么问题可以向我提问,一同进步^_^ 饭卡 Time Limit: 5000/1000 MS (Java ...

  9. C++动态规划求解0-1背包问题

    问题描述: 给定n种物品和一背包.物品i的重量是wi,其价值为vi,背包的容量为C.问:应该如何选择装入背包的物品,是的装入背包中物品的总价值最大? 细节须知: 暂无. 算法原理: a.最优子结构性质 ...

随机推荐

  1. java 中断线程的几种方式 interrupt()

    中断 中断(Interrupt)一个线程意味着在该线程完成任务之前停止其正在进行的一切,有效地中止其当前的操作.线程是死亡.还是等待新的任务或是继续运行至下一步,就取决于这个程序.虽然初次看来它可能显 ...

  2. Gradle打包问题Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0

    前言 使用gradle打包react native的时候,出现了如下报错,下面和大家说一下解决的具体办法 Deprecated Gradle features were used in this bu ...

  3. 加秘钥的SSH

    import paramiko private_key = paramiko.RSAKey.from_private_key_file('id_rsa31.txt') # 创建SSH对象 ssh = ...

  4. NGUI的HUD Text的扩展插件学习--(UIFollowTarget)的使用

    一,我们先导入NGUI_HUD_Text_v1.11包,导入包后会在项目生成一个这样的文件夹 二,我们添加一个cube,给cube添加一个空的游戏对象 二,我们使添加一个label,然后给label添 ...

  5. 系统盘(c盘)空间清理方法总结(转)

      我们一般会把系统安装在C盘,但是使用一段时间后会发现C盘的空间越来越少.尤其我们做开发的,会在电脑中装上很多软件的.比如我的机器上C盘空间15G,平时安装软件时只要可以选择我都会把它装到其他盘的, ...

  6. vue axios 拦截器

    前言 项目中需要验证登录用户身份是否过期,是否有权限进行操作,所以需要根据后台返回不同的状态码进行判断. 第一次使用拦截器,文章中如有不对的地方还请各位大佬帮忙指正谢谢. 正文 axios的拦截器分为 ...

  7. 项目常见bug

    Invalid prop: type check failed for prop "disabled". Expected Boolean, got String with val ...

  8. python 安装opencv及问题解决

    正常安装模式 pip install opencv-python==3.4.5.20 pip install opencv-contrib-python==3.4.5.20 -i http://pyp ...

  9. RabbitMQ相关使用命令

    启动:rabbitmq-server -detached 停止:rabbitmqctl stop 状态:rabbitmqctl status 查看所有用户rabbitmqctl list_users ...

  10. 线程屏障CyclicBarrier实现原理

    生产环境中,存在需要等待多个线程都达到某种状态后,才继续运行的情景.并发工具CyclicBarrier就能够完成这种功能.本篇从源码方面,简要分析CyclicBarrier的实现原理. 使用示例 pu ...