HDOJ 2602 Bone Collector 【动态规划】

题目链接:

  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17434



动态规划:

经典的01背包问题

有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。
    求解将哪些物品装入背包可使价值总和最大。

这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。
    用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:
        f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
    将它详细解释一下:
    “将前i件物品放入容量为v的背包中”这个子问题,若只考虑第i件物品的策略(放或不放),那么就可以转化为一个只牵扯前i-1件物品的问题。
    如果不放第i件物品,那么问题就转化为“前i-1件物 品放入容量为v的背包中”,价值为f[i-1][v];
    如果放第i件物品,那么问题就转化为“前i-1件物品放入剩下的容量为v-c[i]的背包中”,此时能获得的最大价值就是f[i-1][v-c[i]]再加上通过放入第i件物品获得的价值w[i]。

先考虑上面讲的基本思路如何实现,肯定是有一个主循环i=1..N,每次算出来二维数组f[i][0..V]的所有值。
    那么,如果只用一个数组 f[0..V],能不能保证第i次循环结束后f[v]中表示的就是我们定义的状态f[i][v]呢?
    f[i][v]是由f[i-1][v]和f[i-1] [v-c[i]]两个子问题递推而来,能否保证在推f[i][v]时(也即在第i次主循环中推f[v]时)能够得到f[i-1][v]和f[i-1] [v-c[i]]的值呢?
    事实上,这要求在每次主循环中我们以v=V..0的顺序推f[v],这样才能保证推f[v]时f[v-c[i]]保存的是状态 f[i-1][v-c[i]]的值。
    伪代码如下:
    for i=1..N
        for v=V..0
            f[v]=max{f[v],f[v-c[i]]+w[i]};
    其中的f[v]=max{f[v],f[v-c[i]]}一句恰就相当于我们的转移方程f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]},因为现在的f[v-c[i]]就相当于原来的f[i-1][v-c[i]]。

最后的f[v]就是问题的最优解了。


 1 #include<cstdio>
 2 #include<cstring>
 3 const int MAXL = 1000+5;
 4 int max(int a, int b){ return a>b ? a : b;}
 5 int t, n, v, value[MAXL], volume[MAXL], dp[MAXL];
 6
 7 int main(){
 8     scanf("%d", &t);
 9     while(t--){
10         scanf("%d%d", &n, &v);
11         for(int i = 1; i <= n; i++) scanf("%d", &value[i]);
12         for(int i = 1; i <= n; i++) scanf("%d", &volume[i]);
13         memset(dp, 0, sizeof(dp));
14         for(int i = 1; i <= n; i++){
15             for(int j = v; j >= volume[i]; j--){ // 注意j的停止条件,一是防止越界,二是小于0后的答案就是dp[j],已经计算过了
16                 //从v遍历至0的逆序可以保证dp[j-volume[i]]是上一层的计算结果,
17                 //又因为dp数组第二维的v只涉及到上一层的数据,其他层的数据都不会再访问
18                 //从而省去dp数组的第一维度,每次都对之前的数据进行刷新,节省空间复杂度至O(n);
19                 dp[j] = max(dp[j], dp[j-volume[i]]+value[i]);
20             }
21         }
22         printf("%d\n", dp[v]);
23     }
24
25     return 0;
26 }

  

时间: 2024-11-18 16:29:39

HDOJ 2602 Bone Collector 【动态规划】的相关文章

HDOJ 2602 Bone Collector【01背包】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34251    Accepted Submission(s): 14101 Problem Description Many years ago , in

hdoj 2602 Bone Collector【0-1背包】【dp思想】

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

hdoj 2602 Bone Collector 【01背包】

意甲冠军:给出的数量和袋骨骼的数,然后给每块骨骼的价格值和音量.寻求袋最多可容纳骨骼价格值 难度;这个问题是最基本的01背包称号,不知道的话,推荐看<背包9说话> AC by SWS 主题链接 http://acm.hdu.edu.cn/showproblem.php?pid=2602 代码: #include<stdio.h> #include<string.h> typedef struct{ int w, v; }str; str s[1005]; int dp[

杭电 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 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 2602 Bone Collector(01背包)模板

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54132    Accepted Submission(s): 22670 Problem Description Many years ago , in

hdu 2602 Bone Collector 【01背包模板】

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 40404    Accepted Submission(s): 16786 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 Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 30486    Accepted Submission(s): 12550 Problem Description Many years ago , in

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