每日一题之 hdu2602

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<=1000,V<=1000)表示骨头的数量和他的袋子的体积。第二行包含N个整数表示每个骨头的价值。第三行包含N个整数,表示每个骨头的体积。
输出能装入骨头的最大价值。

思路:

简单的01背包

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

const int maxn = 1e3 + 5;
long long dp[maxn];

int main()
{
    int t;
    cin >> t;
    int N,V,x;
    vector<int>A;
    vector<int>val;
    while(t--) {
        cin >> N >> V;
        A.clear();
        val.clear();
        memset(dp,0,sizeof(dp));
        for (int i = 0; i < N; ++i) {
            cin >> x;
            val.push_back(x);
        }
        for (int i = 0; i < N; ++i) {
            cin >> x;
            A.push_back(x);
        }
        long long res = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = V; j >= 0; --j){
                if (j >= A[i])
                    dp[j] = max(dp[j],dp[j-A[i]] + (long long)val[i]);
                res = max(res,dp[j]);
            }
        }
        cout << res << endl;

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值