Bone Collector 0-1背包问题

题目描述:

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 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.

输出:

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题目大意:有一个人喜欢收集骨头(呃),现在有N根骨头,每根骨头对应着各自的体积和价值,这个人的背包容量为V,求能装的骨头的最大总价值。代码:
#include<iostream>
#include<stdio.h>
#define max(a,b) a>b?a:b
struct bone{
    int wei,val;
};
int f[1002][1002];
bone temp[1005];
int value(int num,int v){
    for(int i=1;i<=num;++i){
        for(int j=0;j<=v;++j){
            if(j<temp[i-1].wei)
                f[i][j]=f[i-1][j];
            else
                f[i][j]=max(f[i-1][j-temp[i-1].wei]+temp[i-1].val,f[i-1][j]);
        }
    }
    return f[num][v];
}
using namespace std;
int main(){
    int n,num,v;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d%d",&num,&v);
        for(int j=0;j<num;++j) scanf("%d",&temp[j].val);
        for(int j=0;j<num;++j) scanf("%d",&temp[j].wei);
        printf("%d\n",value(num,v));
    }
    return 0;
}

这是一道0-1背包问题,要解决的问题就是要放哪些物品进背包,总价值是多少。

而对于每一个物品只有放与不放两种情况:

1. 第i件放进去:价值为:f[i-1][v-temp[i-1].wei]+temp[i-1].val(背包容量减去第i件物品的体积,再加上对前i-1件进行判断后得到价值的最大值(容量为v-temp[i-1].wei的背包))

2. 第i件不放进去:价值为:f[i-1][v](容量不减少(为v),对前i-1件进行判断后得到价值的最大值)

状态转移方程为:f[i][v] = max(f[i-1][v], f[i-1][v-w[i]]+c[i])

随后对两种情况取较大值。第一行初始化为0.从第一个物品开始列表记录背包容量内剩余不同容积存放前i个物品情况的最大值。

这个解法比较容易理解,下面的链接有对背包问题更详尽的解释,受启发于此:

https://www.cnblogs.com/A-S-KirigiriKyoko/p/6036368.html

https://www.cnblogs.com/xym4869/p/8513801.html

(其中的代码将第一列也初始化为0,这样子的话当物品中同时存在体积=背包容量的物品和体积=0的物品时就会出错,因此稍作改动)

原文地址:https://www.cnblogs.com/-y-i-/p/10206060.html

时间: 2024-11-06 03:48:06

Bone Collector 0-1背包问题的相关文章

HDU 2602 Bone Collector 0/1背包

题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28903    Accepted Submission(s): 11789 Problem Description Many years ago , in Teddy's h

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): 2665    Accepted Submission(s): 1392 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

HDU 2602 Bone Collector (01背包问题)

原题代号:HDU 2602 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 原题描述: 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 ,

hdu2602 Bone Collector(背包问题)

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

0-1背包问题(经典)HDU2602 Bone Collector

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

hdu2602Bone Collector ——动态规划(0/1背包问题)

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

Bone Collector

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

Bone Collector(杭电2602)(01背包)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31604    Accepted Submission(s): 13005 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个有序数组的头,然后比