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 ...
随机推荐
- php多线程thread开发与应用的例子
Php多线程的使用,首先需要PHP5.3以上版本,并安装pthreads PHP扩展,可以使PHP真正的支持多线程,扩展如何安装请自行百度 PHP扩展下载:https://github.com/kra ...
- jacob访问ocx控件方法和遇到的问题
最近在进行摄像机的二次开发,摄像机厂商提供了使用C++开发的ocx控件:所以尝试使用jacob来进行访问. 操作步骤如下: 1, 从官网(http://sourceforge.net/projects ...
- Delphi 中的DLL 封装和调用对象技术(刘艺,有截图)
Delphi 中的DLL 封装和调用对象技术本文刊登2003 年10 月份出版的Dr.Dobb's 软件研发第3 期刘 艺摘 要DLL 是一种应用最为广泛的动态链接技术但是由于在DLL 中封装和调用对 ...
- 146. LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- Redisson使用起来很方便,但是需要redis环境支持eval命令
Redisson使用起来很方便,但是需要redis环境支持eval命令,否则一切都是悲剧,比如me.结果还是要用RedisCommands去写一套.例子就如下,获得一个RLock锁对象,然后tryLo ...
- push本地代码到github出错
$ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] master -> master (non-f ...
- SharePoint 2013版本功能对比介绍
转:http://www.fengfly.com/plus/view-213720-1.html 在SharePoint使用中,经常纠结于版本问题,SharePoint 2013主要有免费的Found ...
- ruby编程语言-学习笔记3(第4章 表达式和操作符)
4.6 操作符 了解优先级很重要 位移操作符 (0b1011)<< 1 # ==> "10110" 11 << 1 = 22 ...
- MySQL运行原理与基础架构
1.MySQL基础 MySQL是一个开放源代码的关系数据库管理系统.原开发者为瑞典的MySQL AB公司,最早是在2001年MySQL3.23进入到管理员的视野并在之后获得广泛的应用. 2008年My ...
- modsecookie
Cookie parsing added添加cookie解析 现在你可以使用新的可选的过滤器变量(COOKIE_name, COOKIE_NAMES, COOKIE_VALUES)分析cookies. ...