Bone Collector(01背包+记忆化搜索)
Bone Collector
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 21 Accepted Submission(s) : 6
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 ?

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.
total value (this number will be less than 2[sup]31[/sup]).
5 10
1 2 3 4 5
5 4 3 2 1
#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
int f[];
int val[];
int cos[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(f,,sizeof(f));
memset(val,,sizeof(val));
memset(cos,,sizeof(cos));
int n,v;
scanf("%d%d",&n,&v);
int i,j;
for(i=;i<=n;i++)
scanf("%d",&val[i]);
for(j=;j<=n;j++)
scanf("%d",&cos[j]);
for(i=;i<=n;i++)
{
for(j=v;j>=cos[i];j--)
{
// f[j]=f[j-1];
// if(j>=cos[i])
f[j]=max(f[j],f[j-cos[i]]+val[i]);
}
}
printf("%d\n",f[v]);
}
return ;
}
记忆化搜索;
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=;
int dp[MAXN][MAXN],w[MAXN],p[MAXN];
int dfs(int i,int v){
if(dp[i][v])return dp[i][v];
if(i==||v<)return ;//
if(w[i]>v)dp[i][v]=dfs(i-,v);
else dp[i][v]=max(dfs(i-,v),dfs(i-,v-w[i])+p[i]);//
return dp[i][v];
}
int main(){
int T,N,M;
SI(T);
while(T--){
SI(N);SI(M);
for(int i=;i<=N;i++)SI(p[i]);
for(int i=;i<=N;i++)SI(w[i]);
mem(dp,);
printf("%d\n",dfs(N,M));
}
return ;
}
Bone Collector(01背包+记忆化搜索)的更多相关文章
- 01背包-记忆化搜索到成型的DP
记忆化搜索 #include<bits/stdc++.h> using namespace std; typedef long long ll; int n,W; int dp[105][ ...
- HDU 2602 Bone Collector(01背包裸题)
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu2602 Bone Collector 01背包
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like ...
- HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- [原]hdu2602 Bone Collector (01背包)
本文出自:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //=================================== ...
- hdu2602 Bone Collector (01背包)
本文来源于:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //================================== ...
- HDU 2602 Bone Collector --01背包
这种01背包的裸题,本来是不想写解题报告的.但是鉴于还没写过背包的解题报告.于是来一发. 这个真的是裸的01背包. 代码: #include <iostream> #include < ...
- ACM HDU Bone Collector 01背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 这是做的第一道01背包的题目.题目的大意是有n个物品,体积为v的背包.不断的放入物品,当然物品有 ...
- 解题报告:hdu2602 Bone collector 01背包模板
2017-09-03 15:42:20 writer:pprp 01背包裸题,直接用一维阵列的做法就可以了 /* @theme: 01 背包问题 - 一维阵列 hdu 2602 @writer:ppr ...
随机推荐
- 新建Android工程没有自动生成R.JAVA,应该先升级下ADT
前几天非常郁闷,本来计划在Android上做个小东西,结果打开Eclipse新建工程,发现居然没有R.JAVA! 反复测试很多次,均未成功,最后试着升级了下ADT,结果搞定,在这里记下,下次遇到这样的 ...
- D、GO、Rust 谁会在未来取代 C?为什么?——Go语言的定位非常好,Rust语言非常优秀,D语言也不错
不要管我的地位和 D 语言创造者之一的身份.我会坦诚的回答这个问题.我熟悉 Go 和 Rust,并且知道 D 的缺点在哪里.我鼓励人们在 Rust 和 Go 社区相似身份的人,也可以提出他们诚恳的观点 ...
- LINUX 暂停、继续进程
LINUX 暂停.继续进程 kill -STOP 1234 将该进程暂停. 如果要让它恢复到后台,用kill -CONT 1234 (很多在前台运行的程序这样是不行的) 如果要恢复到前台,请在当时运行 ...
- FTP的主动模式和被动模式
摘自http://blog.csdn.net/love_gaohz/article/details/50723164 http://my.oschina.net/binny/blog/17469 FT ...
- python 魔法方法之:__getitem__ __setitem__ __delitem__
h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...
- python安装完毕后,提示找不到ssl模块的解决方示
python安装完毕后,提示找不到ssl模块: [root@localhost ~]# python2.7.5 Python 2.7.5 (default, Jun 3 2013, 11:08:43) ...
- 下载类网站的SEO优化方面技巧
在互联网国际中有一类十分主要的网站,那即是供应各种软件下载的网站,这类网站可以协助用户解决许多软件运用方面的疑问,可是随着知识产权维护的认识越来越强,许多下载类网站也要开端改动自个的经营策略,这么才可 ...
- as3 页游中,新手指导中,屏蔽所有交互对象,但除了指定交互对象可用的方法【转http://blog.csdn.net/linjf520/article/details/9450945】
package { import flash.display.InteractiveObject; import flash.display.Stage; import flash.events.Mo ...
- LeetCode Day2
Power of Two /** * LeetCode: Power of Two * Given an integer, write a function to determine if it is ...
- IE6、7下获得offset值跟其他浏览器不一样问题
大家都知道,offset是元素的位置坐标,那位置坐标又和文档流有关系.如果position默认不设置的话,其值是static.static是个什么东东呢?下面我详细介绍一下: 语法: position ...