HDU2602(01背包)

Bone Collector

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

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

01背包模板题

/*
    Accepted    2602    78MS    5360K    552 B    G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=1005;
int dp[MAXN][MAXN];
int n,W;
int v[MAXN],w[MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&W);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)    scanf("%d",&v[i]);
        for(int i=0;i<n;i++)    scanf("%d",&w[i]);

        for(int i=0;i<n;i++)
            for(int j=0;j<=W;j++)
                if(j<w[i])    dp[i+1][j]=dp[i][j];
                else dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);

        printf("%d\n",dp[n][W]);
    }

    return 0;
}

贴一个记忆化搜索

/*
    Accepted    2602    327MS    5432K    613 B    G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=1005;
int dp[MAXN][MAXN];
int n,W;
int v[MAXN],w[MAXN];
int dfs(int i,int wei)
{
    if(dp[i][wei])    return dp[i][wei];
    if(i==n)    return 0;
    int res;
    if(wei>=w[i])    res=max(dfs(i+1,wei-w[i])+v[i],dfs(i+1,wei));
    else    res=dfs(i+1,wei);
    return dp[i][wei]=res;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&W);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)    scanf("%d",&v[i]);
        for(int i=0;i<n;i++)    scanf("%d",&w[i]);
        printf("%d\n",dfs(0,W));
    }

    return 0;
}
时间: 2024-12-14 17:58:34

HDU2602(01背包)的相关文章

HDU2602 01背包 xingxing在努力

很经典的01背包, 假设f(i, j)是将i个物品放入容量为j的背包, 那么可得到如下递推式f(i, j) = max(f(i-1, j) , f(i-1, j-c[i])+v[i]))....实现的话有两种方式, 一种是直接用二维数组实现, 另外一种是滚动数组, 不过要注意的是, 如果题意是让这些物品恰好装满背包,那么f(0, 0)为0其他为无穷大, 如果没必要恰好装满那就全部初始化为0: 代码如下: #include <cstdio> #include <cstring> #i

hdu2602 01背包Bone Collector

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

hdu2602 Bone Collector (01背包)

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

HDU2602 Bone Collector 【01背包】

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

HDU2602 (0-1背包)

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

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

17-又见01背包

/*                                        又见01背包时间限制:1000 ms  |  内存限制:65535 KB难度:3 描述        有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W     的物品,求所有挑选方案中物品价值总和的最大值.    1 <= n <=100    1 <= wi <= 10^7    1 <= vi <= 100    1 <= W <= 10^

HDU - 2602 Bone Collector(01背包讲解)

题意:01背包:有N件物品和一个容量为V的背包.每种物品均只有一件.第i件物品的费用是volume[i],价值是value[i],求解将哪些物品装入背包可使价值总和最大. 分析: 1.构造二维数组:dp[i][j]---前i件物品放入一个容量为j的背包可以获得的最大价值. dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - volume[i]] + value[i]);---(a) (1)dp[i - 1][j]---不放第i件物品,因此前i件物品放入一个容量为

01背包

这里就只放自己刷的题目了,毕竟是弱弱哒 HDU2546:饭卡 1 #include <algorithm> 2 #include <cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n,m; 9 while (~scanf("%d", &n), n) 10 { 11 int f[2013] = {0}, menu[2013] = {0}; 12 for (int i = 1; i <