Bone Collector(01背包,模板题)

C - Bone Collector

Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

SubmitStatusPracticeHDU
2602

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 2
31).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1 

Sample Output

14 

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 1010;

int n, vb;
int r[MAXN], v[MAXN];
int dp[MAXN][MAXN];

int rec(int i, int j)
{
    if (dp[i][j] >= 0)
    {
        return dp[i][j];
    }

    int res;

    if (i == n)
    {
        res = 0;
    }
    else if (j < r[i])
    {
        res = rec(i + 1, j);
    }
    else
    {
        res = max (rec(i + 1, j), rec(i + 1, j - r[i]) + v[i]);
    }

    return dp[i][j] = res;
}

int main()
{
    int t;

    scanf("%d", &t);

    while (t--)
    {
        memset(r, 0, sizeof (r));
        memset(v, 0, sizeof (v));
        memset(dp, -1, sizeof (dp));

        scanf("%d%d", &n, &vb);

        for (int i=0; i<n; i++)
        {
            scanf("%d", &v[i]);
        }

        for ( int i=0; i<n; i++)
        {
            scanf("%d", &r[i]);
        }

        printf("%d\n", rec(0, vb));
    }

    return 0;
}

一次A了!

时间: 2024-10-18 15:53:09

Bone Collector(01背包,模板题)的相关文章

HDU 2602 Bone Collector(01背包裸题)

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

hdu 2602 Bone Collector 01背包模板

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目要求是尽量装的最多,所以初始化的时候都为0即可. 如果要求恰好装满,初始化的时候除了dp[0] = 0,其他都要设成-inf,表示不合法情况. 1 #include <bits/stdc++.h> 2 using namespace std; 3 int T; 4 int dp[1010][1010]; 5 int v[1010], w[1010]; 6 int main() 7 { 8

1085 背包问题(0-1背包模板题)

1085 背包问题(0-1背包模板题)(51NOD基础题) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整数),与之相对应的价值为P1,P2--Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数,N和W中间用空格隔开.N为物品的数量,W为背包的容量.(1 <= N <= 100,1 <= W <= 10000) 第2 - N + 1行,每行

hdu2602 Bone Collector (01背包)

本文出自:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //============================================================================ // Name : 2602.cpp // Author : vit // Version : // Copyright : Your copyright notice // Description : Hello

hdu 2602 Bone Collector 01背包

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 50442    Accepted Submission(s): 21153 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bon

HDU 2602 Bone Collector (01背包DP)

题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>

hdoj 2620 Bone Collector(0-1背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 思路分析:该问题为经典的0-1背包问题:假设状态dp[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值,则可以推导出dp递推公式 dp[i][v] = Max{dp[i-1][v], dp[i-1][v – c[i]] + w[i]}:c[i]表示第i件物品的容量,w[i]表示第i件物品的价值:该动态规划问题每个阶段的决策为是否要 选择第i件物品放入背包中,如果不选择第i件物

You Like Cake(超大01背包模板题)

You Like Cake 题目描述 双十一就要来啦!而Yuno刚刚获得了一笔X元的奖金.那么是不是应该清空下购物车呢? 购物车总共有N个物品,每个物品的价格为Vi,Yuno想尽可能地把手头的奖金给花光,所以她要精心挑选一些商品,使得其价格总和最接近但又不会超过奖金的金额.那么Yuno最后最少可以剩下多少钱呢? 输入 第一行,两个正整数N和X. 第二行,N个正整数Vi表示第i个物品的价格. 输出 输出一个整数,表示Yuno最后最少可以剩下的钱数. 样例输入 4 50 1 2 3 4 样例输出 4

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro

D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses 分组背包模板题

http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判断. 这样是不对的.因为这样使得同一组里面可能选择了两次. 3 0 2 1 2 3 1 1 3 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include &