dp重拾-01背包--HDU 2602

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

挺久没刷DP题了,先来一个01背包预热一下

#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int dp[1001][1001];
int w[1001],v[1001],W,n;
int max(int a,int b)
{
    if(a>=b) return a;
    else return b;
}
void solve()
{
    for(int i=n-1;i>=0;i--)
    {
        for(int j=0;j<=W;j++)
        {
            if(j<w[i]) dp[i][j]=dp[i+1][j];
            else dp[i][j]=max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]);
        }
    }
    cout<<dp[0][W]<<endl;
}

int main()
{
    int i,j,k;
    scanf("%d",&k);
    for(i=0;i<k;i++)
    {
        scanf("%d %d",&n,&W);
        for(j=0;j<n;j++)
        {
            scanf("%d",&v[j]);
        }
        for(j=0;j<n;j++)
        {
            scanf("%d",&w[j]);
        }
        solve();
        W=n=0;
        memset(dp,0,sizeof(dp));
        memset(v,0,sizeof(v));
        memset(w,0,sizeof(w));
    }
    return 0;
}

时间: 2024-08-09 10:41:49

dp重拾-01背包--HDU 2602的相关文章

dp重拾-完全背包--HDU 4508

减肥记 湫湫给了你每日食物清单,上面描述了当天她想吃的每种食物能带给她的幸福程度,以及会增加的卡路里量. Input 输入包含多组测试用例. 每组数据以一个整数n开始,表示每天的食物清单有n种食物. 接下来n行,每行两个整数a和b,其中a表示这种食物可以带给湫湫的幸福值(数值越大,越幸福),b表示湫湫吃这种食物会吸收的卡路里量. 最后是一个整数m,表示湫湫一天吸收的卡路里不能超过m. [Technical Specification] 1. 1 <= n <= 100 2. 0 <= a

dp背包问题/01背包,完全背包,多重背包,/coin change算法求花硬币的种类数

一步一步循序渐进. Coin Change 具体思想:给你 N元,然后你有几种零钱S={S1,S2...,Sm} (每种零钱数量不限). 问:凑成N有多少种组合方式  即N=x1 * S1+x2*S2+...+xk*Sk (xk>=0,k=1,2..m) 设有f(x)中组合方式 有两种解答(自底向上回溯): 1.不用第m种货币   f(N,m-1) 2.用第m种货币 f(N-Sm,m) 总的组合方式为f(N,m)=f(N,m-1)+f(N-Sm,m) anything is nonsense,s

P2347 砝码称重 (01背包)

题目描述 设有 1g1g1g . 2g2g2g . 3g3g3g . 5g5g5g . 10g10g10g . 20g20g20g 的砝码各若干枚(其总重 ≤1000 \le 1000≤1000 ), 输入输出格式 输入格式: 输入方式: a1,a2,a3,a4,a5,a6a_1 , a_2 ,a_3 , a_4 , a_5 ,a_6a1?,a2?,a3?,a4?,a5?,a6? (表示 1g1g1g 砝码有 a1a_1a1? 个, 2g2g2g 砝码有 a2a_2a2? 个,-, 20g20g

01背包 HDU 2639,第K优解

只要每次都保存前k优解就可以了 注意当价值一样时,只算一种,所以要进行去重复 的操作 用到了unique, 1 2 2 4 0 0 unique之后1 2 4 0 0 2 Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2433    Accepted Submission(s): 1277 Prob

【DP基础】01背包-复习

1066: 0/1 背包 时间限制: 1 Sec内存限制: 128 MB 题目描述 一个旅行者有一个最多能用m公斤的背包,现在有n件物品,它们的重量分别是W1,W2,...,Wn,它们的价值分别为 C1,C2,...,Cn.若每种物品只有一件,求旅行者能获得最大总价值. 输入 第一行:两个整数,M(背包容量,M<=200)和N(物品数量,N<=30):第2..N+1行:每行二个整数Wi,Ci,表示每个物品的重量和价值. 输出 仅一行,一个数,表示最大总价值. 样例输入 10 4 2 1 3 3

【DP复习】01背包 ovo

水题10s切得pj-的那种QAQ P1048 采药 1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 const int sz = 1010; 5 int dp[sz], val[sz], tim[sz]; 6 int t, m; 7 int main() { 8 scanf("%d%d", &t, &m); 9 for(int i = 1; i <= m; i

0-1背包 VIOJ1025

P1025小飞侠的游园方案 请 登录 后递交 标签:[显示标签] 描述 菜虫:你的题目是--我们的情报组织探听到敌人的重要将领--小飞侠星期天会邀他的灵儿妹妹到公园去玩.公园里有很多娱乐项目,可并不是每一项他们都喜欢,所以他们对每一项都进行了"喜欢度"的评分.因为小飞侠也是一个了不起的角色,所以他一定会选择在有限时间内的最好的方案.现在要你做的就是找出在规定时间内他们选择哪几项不同的活动可以使其"喜欢度"之和达到最大,据此我们就可以知道他会在哪些地方出现,从而在那里

hdu 2602 dp 01背包

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 这题是非常标准的01背包,没啥特殊的地方,很简单 代码: #include <bits/stdc++.h> #define MAXS 1006 using namespace std; int value[MAXS]; int main () { int T; int n,m_v,v; int f[MAXS] ; cin >> T; while(T--) { memset(f,

HDU 2602 Bone Collector (01背包DP)

题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>