SDNU 1539 Do you like Hot Dog ?

Description

Hot dog is a very delicious food,and Goc like it very much. The picture below shows how much does Goc like it. hhhhh...
Given a set of hot dog, each with a price and a happy value , determine a way to choose the items into a knapsack so that the total price is less than or equal to a given limit ​ and the total happy value is as large as possible. Find the maximum total happy value. (Note that each item can be only chosen once).

Input

The first line contains the integer ​ indicating to the number of test cases.

For each test case, the first line contains the integers ​ and ​.

Following ​ lines provide the information of each item.

The line contains the price and the happy value of the ​ item respectively.

 

 

 

All the inputs are integers.

Output

For each test case, output the maximum value.

Sample Input

1
5 15
12 4
2 2
1 1
4 10
1 2

Sample Output

15
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
#define ll long long
int t, n, pri, p[500+8], v[500+8], dp[500+8][5000+8], sum;
void solve()
{
    fill(dp[0], dp[0] + 5005, 0x3f3f3f3f);
    dp[0][0] = 0;
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j <= 5001; j++)
        {
            if(j<v[i]) dp[i+1][j] = dp[i][j];
            else dp[i+1][j] = min(dp[i][j],dp[i][j-v[i]]+p[i]);
        }
    }
    int miao = 0;
    for(int i = 0; i <= 5001; i++)
    {
        if(dp[n][i] <= pri) miao = i;
    }
    printf("%d\n", miao);
}
int main()
{
    scanf("%d", &t);
    memset(dp, 0, sizeof(dp));
    memset(p, 0, sizeof(p));
    memset(v, 0, sizeof(v));
    while(t--)
    {
        scanf("%d%d", &n, &pri);
        for(int i = 0; i<n; i++)
        {
            scanf("%d%d", &p[i], &v[i]);
        }
        solve();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/RootVount/p/10326113.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值