Bone Collector(01背包问题入门)
来源:互联网 发布:解压缩软件手机版 编辑:程序博客网 时间:2024/05/22 15:38
Bone Collector
原题连接:点击打开链接
Total Submission(s): 15290 Accepted Submission(s): 6055
Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
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 ?
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
The first line contain a integer T , the number of 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.
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
One integer per line representing the maximum of the total value (this number will be less than 231).
Sample Input
15 101 2 3 4 55 4 3 2 1
Sample Output
14
Author
Teddy
Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest
Recommend
lcy
#include <iostream>#include <string>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <map>#include <vector>#include <queue>#include <stack>#define LL long long#define MAXI 2147483647#define MAXL 9223372036854775807#define eps (1e-8)#define dg(i) cout << "*" << i << endl;using namespace std;int vol[1001], val[1001], maxVal[1001][1001];int max (const int a, const int b) {return a > b ? a : b;}int main(){ int T, N, V, ans; scanf("%d", &T); while(T--) { scanf("%d %d", &N, &V); for(int i = 1; i <= N; ++i) scanf("%d", &val[i]); for(int i = 1; i <= N; ++i) scanf("%d", &vol[i]); if(V == 0) { ans = 0; for(int i = 1; i <= N; ++i) if (!vol[i]) ans += val[i]; } else if(N == 0) ans = 0; else { for(int i = 1; i <= N; ++i) { for(int j = 0; j <= V; ++j) { //if(i == 0 || j == 0) maxVal[i][j] = 0; //else…… /*上面的语句是错误的,当j等于0时,表明此时背包容量为0,按理说不能装任何东西,故而 maxVal[i][0] = 0,但是,在这题中,物品的体积可为0,故此背包容量为0但是还是可以装物品。 可以先设maxVal[i][0] = 0,但是由于使用if-else语句,设maxVal[i][0] = 0之后就不执行了, 无法更新maxVal[i][0] = 0*/ if(vol[i] > j) maxVal[i][j] = maxVal[i - 1][j]; else maxVal[i][j] = max(maxVal[i - 1][j], maxVal[i - 1][j - vol[i]] + val[i]); } } ans = maxVal[N][V]; } printf("%d\n", ans); } return 0;}
- Bone Collector(01背包问题入门)
- hdu2602 Bone Collector(01背包+入门)
- hdu2602 Bone Collector(01背包问题)
- HDU-Bone Collector(01背包问题)
- Bone Collector(01背包问题)
- 01-背包问题---Bone Collector
- 背包问题(2)01背包 Bone Collector
- Bone Collector(01背包)
- Bone Collector(01背包)
- Bone Collector(01背包)
- Bone Collector(01背包)
- Bone Collector(01背包)
- 01背包(Bone Collector)
- Bone Collector(01背包)
- Bone Collector (01背包)
- Bone Collector(01背包)
- 背包问题-Bone Collector
- hdu 2602 Bone Collector (01背包 入门)
- spring3.0+cxf webservice开发
- eclipse装cdt编译c++程序控制台看不到结果的解决方法
- 关于腾讯面试的一道题目
- VC++2012编程演练数据结构《31》狄杰斯特拉算法
- UVa 10055 - Hashmat the Brave Warrior
- Bone Collector(01背包问题入门)
- 未装驱动,收不到WM_DEVICECHANGE怎么办?
- perl s///中使用变量
- Apache+Tomcat集群配置
- HOJ 2111 Electrical Outlets
- JDK 7 中的 Fork/Join 模式
- MC55开发之GPRS远程登陆程序的具体设计(独家整理!保证是你见过的MC55开发最全的笔记~~)
- How to get url not reached error in Anodroid Phonegap
- Ubuntu KDE安装