Bone Collector------HDOJ杭电2602(纯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 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

先输入一个t,代表有t个样例。然后输入n和v,n代表有多少骨头。v代表背包体积,每样东西仅仅有一个,也就是说仅仅能取一次,刚開始看这道题的时候全然不知道怎么做。感觉会非常麻烦的样子,学长是作为例题给我们讲的。刚開始有点头绪,但详细还是全然不懂。后来自己专门刷这方面的专题。还算理解的比較好。动态规划的思想得要理解好,这题是用空间换时间。过程中会产生大量之间数据,嗯,就是这样。好了,回到这道题。我们用一维数组来存储数据,可是有些pdf文档比方背包九讲就是用二维数组来解说,都能够的。

如今进入正题:输入前面讲过了,如今讲核心代码

for(i=1;i<=n;i++)
   for(j=v;j>=c[i];j--)
        liu[j]=max(liu[j],liu[j-c[i]]+w[i]);

这三行就是核心。就像背包九讲里面说的,这个状态转移方程很重要,一定要理解,它联系了上一个状态和这一个状态,所以叫做状态转移方程!

!!!

!!

为了更加清楚描写叙述执行过程中数组每一个值的详细变化,我在这里加了几行代码:

for(i=1;i<=n;i++)
        {
            for(j=v;j>=c[i];j--)
            {
                liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
            }
            for(k=1;k<=v;k++)
                printf("%d ",liu[k]);
            printf("\n");
        }

我们以题目给的数据为例,执行结果例如以下:

从图中我们能够看出(结合上面的代码),程序循环五次,每次循环的结果都在动态变化。假设还不能理解,建议自己在草稿子上模拟i=1。i=2的时候数组变化的情况,就会非常好理解了。动态规划给我的感觉就是代码非常短,可是理解之后就非常easy了!!!

!!!

好了。讲完了,近期在做dp专题,会不定时更新做题的心得还有题解,大家一起学习。

以下贴下AC代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
    int i,j,k;
    int t,n,m,v;
    int liu[1006],c[1006],w[1006];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&v);
        for(i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&c[i]);
        memset(liu,0,sizeof(liu));
        for(i=1;i<=n;i++)
        {
            for(j=v;j>=c[i];j--)
            {
                liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
            }
            for(k=1;k<=v;k++)
                printf("%d ",liu[k]);
            printf("\n");
        }
        printf("%d\n",liu[v]);
    }
    return 0;
}

写代码能力有限,如有编程爱好者发现bug,还请指出,不胜感激!

时间: 2024-11-06 19:14:23

Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)的相关文章

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

杭电 oj2602~(0-1背包问题)

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

Bone Collector------HDOJ杭电2602(纯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

杭电 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

01背包基础 (杭电2602)

01背包问题: 有一个体积为V的背包,有n件物品,每件物品的体积,价值分别为w[i],p[i];要从n件物品中选些放入背包中,使背包里物品的总价值最大. 动态方程:c[i][j]=max(c[i-1][j],c[i-1][j-w[i]]+p[i]). 有关动态方程方面的代码: for (int i = 1; i <= n; i++) { for (int j = 1; j <= total_weight; j++) { if (w[i] > j) { c[i][j] = c[i-1][j

杭电 2602

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

魔咒词典------HDOJ杭电1880(字符串的处理,很简单)

Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词典.当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能:当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒.如果他要的魔咒不在词典中,就输出"what?" Input 首先列出词典中不超过100000条不同的魔咒词条,每条格式

Is It A Tree?------HDOJ杭电1325(两种方法,可以用也可以不用并查集!!!!!!详解)

Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the

杭电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