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 ...
随机推荐
- 同步两台linux服务器时间同步方案
Linux自带了ntp服务 -- /etc/init.d/ntpd,这个服务不仅可以设置让本机和某台/某些机器做时间同步,他本身还可以扮演一个time server的角色,让其他机器和他同步时间. 配 ...
- MongoDB实战指南(一):大数据与云计算
1.1 什么大数据 具体来说,大数据技术涉及到数据的创造,存储,获取和分析,大数据的主要特点有下面几个: 数据量大.一个典型的PC机载2000年前后其存储空间可能有10GB,今天facebook一天增 ...
- cs ip 通过jmp转移命令间接赋值。无法直接对其赋值。
jmp 寄存器 命令 对IP间接赋值.
- 徐汉彬:Web系统大规模并发——电商秒杀与抢购
[导读]徐汉彬曾在阿里巴巴和腾讯从事4年多的技术研发工作,负责过日请求量过亿的Web系统升级与重构,目前在小满科技创业,从事SaaS服务技术建设. 电商的秒杀和抢购,对我们来说,都不是一个陌生的东西. ...
- C# Json处理日期和Table
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ru ...
- find 日常使用
find 查找文件 fidn 目录 参数 find ~ -type f find /log -name "root.log" find . -type f -exec ls -l ...
- mapreduce源码分析总结
一 MapReduce概述 Map/Reduce是一个用于大规模数据处理的分布式计算模型,它最初是由Google工程师设计并实现的,Google已经将它完整的MapReduce论 文公开发布了.其中对 ...
- php.ini 干了些啥?
今天又重新看了一遍php.ini 的各种配置介绍,感觉还是官网说的比较靠谱,朋友,你所要找的,都在这里了. http://www.php.net/manual/zh/ini.core.php
- 利用if else判断是否及格
static void Main(string[] args) { while (true) { string ...
- BZOJ_1010_[HNOI2008]_玩具装箱toy_(斜率优化动态规划+单调队列)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1010 给出\(n\)和\(l\).有\(n\)个玩具,第\(i\)个玩具的长度是\(c[i]\ ...