Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)
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.
1
5 10
1 2 3 4 5
5 4 3 2 1
14
for(i=1;i<=n;i++)
for(j=v;j>=c[i];j--)
liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
这三行就是核心。就像背包九讲里面说的,这个状态转移方程很重要,一定要理解,它联系了上一个状态和这一个状态,所以叫做状态转移方程!
!!!
!!
for(i=1;i<=n;i++)
{
for(j=v;j>=c[i];j--)
{
liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
}
for(k=1;k<=v;k++)
printf("%d ",liu[k]);
printf("\n");
}
我们以题目给的数据为例,执行结果例如以下:
!!!
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
int t,n,m,v;
int liu[1006],c[1006],w[1006];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&v);
for(i=1;i<=n;i++)
scanf("%d",&w[i]);
for(i=1;i<=n;i++)
scanf("%d",&c[i]);
memset(liu,0,sizeof(liu));
for(i=1;i<=n;i++)
{
for(j=v;j>=c[i];j--)
{
liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
}
for(k=1;k<=v;k++)
printf("%d ",liu[k]);
printf("\n");
}
printf("%d\n",liu[v]);
}
return 0;
}
写代码能力有限,如有编程爱好者发现bug,还请指出,不胜感激!
Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)的更多相关文章
- 杭电 2602 Bone Collector
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 杭电2602 Bone Collector
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 2602 ACM 杭电 骨头容器 01背包
题意:装骨头的容器大小固定,有一堆骨头,已知骨头的价值和大小,在不超过容积大小的情况下,问:所装骨头的最大价值? 思路:典型的01背包问题,不需要有任何的变动. 模板: for(int j=v;j&g ...
- 2955 ACM 杭电 抢银行 01背包 乘法
题意: 强盗抢银行,在不被抓住的情况下,想尽量多的偷点钱.已知各个银行的金钱和被抓的概率,以及强盗能容忍的最大不被抓的概率(小于等于该概率才能不被抓),求最多能抢到钱? 并不是简单的01背包问题? 1 ...
- HDOJ 2546饭卡(01背包问题)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如 ...
- 杭电2602 Bone Collector 【01背包】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 解题思路:给出一个容量为V的包,以及n个物品,每一个物品的耗费的费用记作c[i](即该物品的体积 ...
- 饭卡------HDOJ杭电2546(还是01背包!!!!!!)
Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩 ...
- [HDOJ2639]Bone Collector II(第k优01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:求01背包的第k优解 dp(i, j)表示容量为j时的i优解 对于第二维的操作和01背包几 ...
- 杭电 1114 Piggy-Bank 完全背包问题
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- POJ1269+直线相交
求相交点 /* 线段相交模板:判相交.求交点 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
- Drawable(3)Color State List Resource
注意 Color State List Resource 与 Color不同,前者是颜色状态表.根据不同状态显示不同颜色,它是State list,里面有多种颜色,后者只是一个颜色. Color St ...
- Windows7 64下MinGW64/MSYS环境搭建
原文出处: CompileGraphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64 http://www.kinetic ...
- Android开发UI之Navigation Drawer
http://blog.csdn.net/xyz_lmn/article/details/12523895
- hadoop2.2编程:各种API
hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...
- vim解决中文显示乱码问题
命令:vim ~/.vimrc 写入如下: set enc=utf-8 set fileencoding=utf-8 set fileencodings=ucs-bom,utf8,prc set gu ...
- C#创建Excel文件并将数据导出到Excel文件
工具原料: Windows 7,Visual Studio 2010, Microsoft Office 2007 创建解决方案 菜单>新建>项目>Windows窗体应用程序: 添加 ...
- BZOJ_1096_[ZJOI2007]_仓库建设_(斜率优化动态规划+单调队列+特殊的前缀和技巧)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1096 有\(n\)个工厂,给出第\(i\)个工厂的到1号工厂的距离\(x[i]\),货物数量\ ...
- Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞
漏洞名称: Linux Kernel ‘exitcode_proc_write()’函数本地缓冲区溢出漏洞 CNNVD编号: CNNVD-201311-061 发布时间: 2013-11-07 更新时 ...
- 【转】objective-c基本数据类型之输出格式符
原文网址:http://blog.csdn.net/mamong/article/details/8255691 基本数据类型 1. int 输出格式符:%i, %d, %o %x, 2. float ...