HDU2602 (0-1背包问题)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
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
Sample Input
Sample Output
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[][];
int a[],b[];
int main()
{
int t,n,v;
cin>>t;
while(t--)
{
cin>>n>>v;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<=v;j++)
{
dp[i][j]=(i==?:dp[i-][j]);
if(j>=b[i])
dp[i][j]=max(dp[i-][j],dp[i-][j-b[i]]+a[i]);
}
}
printf("%d\n",dp[n][v]);
}
return ;
}
HDU2602 (0-1背包问题)的更多相关文章
- 蓝桥杯 0/1背包问题 (java)
今天第一次接触了0/1背包问题,总结一下,方便以后修改.不对的地方还请大家不啬赐教! 上一个蓝桥杯的例题: 数据规模和约定 代码: import java.util.Scanner; public ...
- 经典递归问题:0,1背包问题 kmp 用遗传算法来解背包问题,hash表,位图法搜索,最长公共子序列
0,1背包问题:我写笔记风格就是想到哪里写哪里,有很多是旧的也没删除,代码内部可能有很多重复的东西,但是保证能运行出最后效果 '''学点高大上的遗传算法''' '''首先是Np问题的定义: npc:多 ...
- Java实现动态规划法求解0/1背包问题
摘要: 使用动态规划法求解0/1背包问题. 难度: 初级 0/1背包问题的动态规划法求解,前人之述备矣,这里所做的工作,不过是自己根据理解实现了一遍,主要目的还是锻炼思维和编程能力,同时,也是为了增进 ...
- HDU-1864&&HDU-2602(01背包问题)
DP-01背包问题例题 输入处理有点恶心人,不过处理完后就是简单的DP了 从头开始dp[i]表示从0开始到i的最优结果,最后从都边里dp数组,求得最大的报销额. 对于每个i都要从头维护最优结果.(二刷 ...
- 0/1背包问题(DP)
Description 给定 n 个物品和一个背包.物品 i 的重量是 wi ,其价值为 vi ,背包的容量为 C .问:应该如何选择装入背包的物品,使得装入背包中物品的总价值最大? Input 输入 ...
- hdu2602Bone Collector ——动态规划(0/1背包问题)
Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collec ...
- 【Python】0/1背包、动态规划
0/1背包问题:在能承受一定重量的背包中,放入重量不同,价值不同的几件物品,怎样放能让背包中物品的价值最大? 比如,有三件物品重量w,价值v分别是 w=[5,3,2] v=[9,7,8] 包的容量是5 ...
- 使用LINGO来解决0/1背包算法问题
1.问题说明 0/1背包问题:我们有n种物品,物品j的重量为wj,价格为pj.我们假定所有物品的重量和价格都是非负的.背包所能承受的最大重量为W.如果限定每种物品只能选择0个或1个,则问题称为0-1背 ...
- HDU 2602 Bone Collector 0/1背包
题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...
- 动态规划-背包问题 Knapsack
2018-03-15 13:11:12 背包问题(Knapsack problem)是一种组合优化的NP完全问题.问题可以描述为:给定一组物品,每种物品都有自己的重量和价格,在限定的总重量内,我们如何 ...
随机推荐
- matches()方法
java.lang包中的String类和java.util.regex包中的Pattern,Matcher类中都有matches()方法,都与正则表达式有关.下面我分别举例:(字符串:"ab ...
- Windows 10 Technical Preview 10041 使用 IIS Express 运行网站应用程序异常
在 Windows 10 中使用 Visual Studio 2013 Ultimate with Update 4 开发网站,9926 的时候还好好的,升到 10041 就不能调试了: “Syste ...
- 【Android - MD】之TextInputLayout的使用
TextInputLayout是Android 5.0新特性--Material Design中的一个布局控件,主要用来嵌套EditText,实现数据输入时的一些效果,如: 当输入框获取焦点时,输入提 ...
- PHP问题Parse error: syntax error, unexpected end of file in
检查一下你的php文件中是否存在这样的语法错误:<<php{>或者<?{?>以上两种写法都是有错误的,修改为下面的就可以了: <?php}?>
- WebKit历史项管理的实现
历史项管理依据标准定义,由Page管理一个Joint Session History, 包括了各个子Frame的历史项.逻辑上相应例如以下的关系: 从上面看三个层次:Page,Frame,以及JS B ...
- QT+QT creator+OpenCV图像灰度化
1).pro文件 #------------------------------------------------- # # Project created by QtCreator 2014-05 ...
- [RxJS] Combination operators: concat, startWith
Some Observables may complete, and we may want to append another Observable to the one which just co ...
- spring mvc DispatcherServlet详解之三---request通过ModelAndView中获取View实例的过程
整个spring mvc的架构如下图所示: 上篇文件讲解了DispatcherServlet第二步:通过request从Controller获取ModelAndView.现在来讲解第三步:reques ...
- ios--绘图介绍
iOS–绘图介绍 绘制图像的三种方式 一. 子类化UIView,在drawRect:方法画图 执行方法时,系统会自行创建画布(CGContext),并且讲画布推到堆栈的栈顶位置 执行完毕后,系统会执行 ...
- 关于C++的疑问剖析
1)众所周知,抽象类是不存在对象的,只提供接口而不提供实现.但是抽象类能不能作为一个类指针,指向其子类的对象呢? class Interface { public: ; }; class Implem ...