hdoj 2620 Bone Collector(0-1背包)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602

思路分析:该问题为经典的0-1背包问题;假设状态dp[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值,则可以推导出dp递推公式

dp[i][v] = Max{dp[i-1][v], dp[i-1][v – c[i]] + w[i]};c[i]表示第i件物品的容量,w[i]表示第i件物品的价值;该动态规划问题每个阶段的决策为是否要

选择第i件物品放入背包中,如果不选择第i件物品,则dp[i][v] = dp[i-1][v],如果选择第i件物品放入背包,则dp[i][v] = dp[i-1][v-c[i]], v-c[i] >= 0;

同时,可以使用一维数组递推,因为递推i时,只需要使用到第i-1行的数组元素,所以可以从V向下递推到0,递推公式如下: dp[v] = MAX(dp[v], dp[v - c[i]] + w[i]),

(一维dp)代码如下:

import java.util.*;

public class Main {
    static final int MAX_N = 1000 + 10;
    static int[] value = new int[MAX_N];
    static int[] volumn = new int[MAX_N];
    static int[] dp = new int[MAX_N];

    public static int Max(int a, int b) {
        return a > b ? a : b;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int case_times = in.nextInt();
        while (case_times-- != 0) {
            int N, V;

            for (int i = 0; i < MAX_N; ++ i)
                dp[i] = 0;
            N = in.nextInt();
            V = in.nextInt();
            for (int i = 1; i <= N; ++ i)
                value[i] = in.nextInt();
            for (int i = 1; i <= N; ++ i)
                volumn[i] = in.nextInt();
            for (int i = 1; i <= N; ++ i) {
                for (int v = V; v >= 0; -- v) {
                    if (v - volumn[i] >= 0)
                        dp[v] = Max(dp[v], dp[v - volumn[i]] + value[i]);
                }
            }
            System.out.println(dp[V]);
        }
    }
}

(二维dp)代码如下:

import java.util.*;

public class Main {
    static final int MAX_N = 1000 + 10;
    static int[] value = new int[MAX_N];
    static int[] volumn = new int[MAX_N];
    static int[][] dp = new int[MAX_N][MAX_N];

    public static int Max(int a, int b) {
        return a > b ? a : b;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int case_times = in.nextInt();
        while (case_times-- != 0) {
            int N, V;

            for (int i = 0; i < MAX_N; ++ i) {
                for (int j = 0; j < MAX_N; ++ j)
                    dp[i][j] = 0;
            }
            N = in.nextInt();
            V = in.nextInt();
            for (int i = 1; i <= N; ++ i)
                value[i] = in.nextInt();
            for (int i = 1; i <= N; ++ i)
                volumn[i] = in.nextInt();
            for (int i = 1; i <= N; ++ i) {
                for (int v = 0; v <= V; ++ v) {
                    if (v - volumn[i] >= 0)
                        dp[i][v] = Max(dp[i-1][v], dp[i-1][v - volumn[i]] + value[i]);
                    else
                        dp[i][v] = dp[i-1][v];
                }
            }
            System.out.println(dp[N][V]);
        }
    }
}
时间: 2024-12-15 01:38:36

hdoj 2620 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

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[

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个有序数组的头,然后比

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

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

hdu 2602 - Bone Collector(01背包)解题报告

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

【HDU2602】Bone Collector(01背包)

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