美文网首页
hdu-2602 背包问题 骨头收集者

hdu-2602 背包问题 骨头收集者

作者: xcpooo | 来源:发表于2018-12-21 19:05 被阅读0次

Bone Collector

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 89483 Accepted Submission(s): 36838

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

<pre style="overflow-wrap: break-word; white-space: pre-wrap; margin: 0px; font-size: 14px;">

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

#include <iostream>
using namespace std;
int max(int a, int b) {

    return a > b ? a : b;
}



int dp[1005][1005] ;
void main() {
    int t;
    int n;
    int v;
    cin >> t;
    int va[1000], vo[1000];
    while (t--) {
        cin >> n>>v;
            for (int i = 1; i <= n; i++)
                cin >> va[i];
            
            for (int i = 1; i <= n; i++) 
                cin >> vo[i];
            

            memset(dp, 0, sizeof(dp));


            for (int i = 1; i <= n; i++) {
                for (int j = 0; j <= v; j++)//要记得有容量为0的情况
                {
                    if (vo[i] > j) { dp[i][j] = dp[i - 1][j]; }
                    else if(vo[i] <= j) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - vo[i]] + va[i]); }


                }
            }
            cout << dp[n][v] << endl;
        }
    }

相关文章

  • hdu-2602 背包问题 骨头收集者

    Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  • 背包问题(完全背包)

    动态规划合集: 1.矩阵链乘法2.投资组合问题3.完全背包问题4.01背包问题5.最长公共子序列 例题3——背包问...

  • Algorithm进阶计划 -- 动态规划(下)

    经典动态规划背包问题最长子序列问题 1. 背包问题 1.1 0-1 背包问题 0-1 背包问题,描述如下: 上面...

  • 背包问题

    0-1背包问题 问题 n个物体,它们各自有重量和价值,给定一个有容量的背包,如何让背包里装入的物体价值总和最大? ...

  • 背包问题

    问题描述 假如有一个能装下4磅的背包,如何从下列商品列表中选择商品填充背包,使价值达到最大: 动态规划 要解决背包...

  • 背包问题

    (1)0-1背包容量为10的背包,有5种物品,每种物品只有一个,其重量分别为5,4,3,2,1,其价值分别为1,2...

  • 背包问题

  • 背包问题

    01背包(物品只有一个) 有N件物品和一个容量为V的背包。第i建物品的费用是w[i],价值是v[i]。求解将哪些物...

  • 背包问题

    1. 01背包 状态说明:背包体积为v,物品个数为n,f[n,v]表示前n件物品加入背包的最大价值。c_i,w_i...

  • 背包问题

    01背包 优化空间复杂度,变为一维; 外循环依然是n从1->N, 注意内循环 v是从V->0,为什么内循环是V->...

网友评论

      本文标题:hdu-2602 背包问题 骨头收集者

      本文链接:https://www.haomeiwen.com/subject/hbpvkqtx.html