HDU2602Bone Collector 简单0-1背包

Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 48618    Accepted Submission(s): 20269

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 ?

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.

Output

One integer per line representing the maximum of the
total value (this number will be less than 231).

Sample Input

1

5 10

1 2 3 4 5

5 4 3 2 1

Sample Output

14

注意:先输入每个骨头的价值然后输入每个骨头的体积。

状态转移方程:dp[j]=max(dp[j],dp[j-v[i]]+w[i]。

#include <iostream>
#include <string.h>

using namespace std;
int N,V;
int dp[1111];
int w[1111],v[1111];
int main()
{
    int t;
    while(cin>>t){
        while(t--){
            cin>>N>>V;
            for(int i=1;i<=N;i++){
                cin>>w[i];
            }
            for(int i=1;i<=N;i++){
                cin>>v[i];
            }
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=N;i++){
                for(int j=V;j>=v[i];j--){       //j如果小于了v[i]那么v[i]一定无法装入袋子
                    dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
                }
            }
            cout<<dp[V]<<endl;
        }
    }
    return 0;
}
时间: 2024-10-10 02:22:55

HDU2602Bone Collector 简单0-1背包的相关文章

HDU 2602 (简单的01背包) Bone Collector

很标准的01背包问题 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1000 + 10; 8 int w[maxn], v[maxn], dp[maxn]; 9 10 int main(void) 11 { 12 #ifdef LOCAL 13 freopen(&qu

HDU2602Bone Collector

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 29685    Accepted Submission(s): 12208 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

hdu 1864 最大报销额 模型为简单的01背包

很简单的01背包,但是题目还是很蛋疼的注意题目中600,和1000这两个数,还有就是double和int的转换,1A了感觉还是不错的 /************************************************************************* > File Name: hdu1864.cpp > Author: yang > Mail:[email protected] > Created Time: 2014年08月23日 星期六 14:0

Bone Collector(dp 01背包)

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 volu

poj1417 带权并查集+0/1背包

题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是魔. 对于这些问题,我们只需要发现,如果回答对方是魔,那么即可以判断出这两个不是同一种族,而如果回答对方是神,那么说明这两个是同一种族,那么就可以用带权并查集合并这些神和魔,然后记录两种分别多少个,这样当所有询问都处理完时我们就可以得到一系列的集合,每个集合分别有它的两个种族的人数,但是此时对于每个

NOJ 1860 保研(0/1背包概率dp)

保研 时间限制(普通/Java):1000MS/3000MS         运行内存限制:65536KByte 总提交:171          测试通过:40 题目描述 对于一些名校而言,保研不仅可以由学校推免,也可以由学生自己向希望保研的学校提出申请,这个过程有点类似于外国学生向学校提交简历等待Offer的过程.但是,投递申请需要亲自去相应学校的研招办递交材料,这就需要一些成本(比如路费等),且每个院校都有自己的录取成功率.现在,请在总成本不超过限制的情况下,求出最大的成功率. 输入 输入

POJ 1636 Prison rearrangement DFS+0/1背包

题目链接: POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 2194   Accepted: 984 Description In order to lower the risk of riots and escape attempts, the boards of two nearby prisons of equal

POJ 3628 Bookshelf 2 0/1背包和DFS两种解法

题目链接:POJ 3628 Bookshelf 2 Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3436 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly,

牛客网 TaoTao要吃鸡 ( 0/1背包变形 )

题意 : 题目链接 分析 :  如果没有 BUG (即 h == 0 的时候)就是一个普通的 0 / 1 背包 需要讨论一下 h != 0 的情况 此时有就相当于有物品是有特权的 而且背包装有特权的物品根据题目的要求是应当最后装的 也就是说特权物品装完之后背包将不再可装 所以特权物品肯定是只有一个的 数据量并不大,所以可以去枚举这个特权物品 那么如何知道在  没有装特权物品  之前的最佳选择方案? 答案就是用最小的可剩空间留给特权物品,然后其他空间去跑 0/1 背包 最小的可剩空间当然就是 h