杭电 2603 Bone Collector(简单01背包)

Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 35296    Accepted Submission(s): 14531

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

很裸的一道01背包,注意内层for循环(for(int j = v ;j>=weight[i];j--))

AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std ;

int dp[10000] ;
int value[1005],weight[1005] ;
int t,n,v;

int main()
{
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            scanf("%d%d",&n,&v) ;
            for(int i = 0 ;i<n ;i++)
                scanf("%d",&value[i]) ;
            for(int j = 0 ;j<n ;j++)
                scanf("%d",&weight[j]) ;
            memset(dp,0,sizeof(dp)) ;
            for(int i = 0; i<n ;i++)
            {
                for(int j = v ;j>=weight[i] ;j--)
                {
                    dp[j] = max(dp[j],dp[j-weight[i]]+value[i]) ;
                }
            }
            printf("%d\n",dp[v]) ;
        }
    }
    return 0 ;
}
时间: 2024-07-31 04:16:49

杭电 2603 Bone Collector(简单01背包)的相关文章

杭电2602 Bone Collector 【01背包】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 解题思路:给出一个容量为V的包,以及n个物品,每一个物品的耗费的费用记作c[i](即该物品的体积),每一个物品的价值记作w[i], 我们用 f[v]来表示一个容量为v的包的总价值,这样最后我们只需要输出f[V]就能得出结果 则对于第i个物品,它可以放入背包,此时背包的容量变为v-c[i],背包的总价值变为f[v-c[i]]+w[i], 它也可以不放入背包,此时背包的容量还是v,背包的总价值不变

杭电 2602 Bone Collector

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

HDU 2639 Bone Collector II(01背包变型)

此题就是在01背包问题的基础上求所能获得的第K大的价值. 具体做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,其实就是2个数组合并之后排序,但是实际做法最好不要怎么做,因为你不知道总共有多少种,而我们最多只需要前K个大的就行了(因为可能2个数组加起来的组合数达不到K个),如果全部加起来数组开多大不清楚,所以可以选用归并排序中把左右2个有序数组合并成一个有序数组的方法来做,就是用2个变量去标记2个有序数组的头,然后比

杭电 2546 饭卡(01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11285    Accepted Submission(s): 3880 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一

杭电 2639 Bone Collector II【01背包第k优解】

解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[v-c[i]]+w[i]中的最大值,但是现在要求第k大的值,我们就分别用两个数组保留f[v]的前k个值,f[v-c[i]]+w[i]的前k个值,再将这两个数组合并,取第k名. 即f的数组会增加一维. http://blog.csdn.net/lulipeng_cpp/article/details/

hdu 2602 Bone Collector(01背包)

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

杭电2602 Bone Collector

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

HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2470 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

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