F-Beautiful Land(超大背包) 第十四届华中科技大学程序设计竞赛决赛同步赛

链接:https://www.nowcoder.com/acm/contest/119/F
来源:牛客网

题目描述
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
Now HUST got a big land whose capacity is C to plant trees. We have n trees which could be plant in it. Each of the trees makes HUST beautiful which determined by the value of the tree. Also each of the trees have an area cost, it means we need to cost ci area of land to plant.
We know the cost and the value of all the trees. Now HUSTers want to maximize the value of trees which are planted in the land. Can you help them?
输入描述:
There are multiple cases.
The first line is an integer T(T≤10), which is the number of test cases.
For each test case, the first line is two number n(1≤n≤100) and C(1≤C≤108), the number of seeds and the capacity of the land.
Then next n lines, each line contains two integer ci(1≤ci≤106) and vi(1≤vi≤100), the space cost and the value of the i-th tree.
输出描述:
For each case, output one integer which means the max value of the trees that can be plant in the land.
示例1
输入

1
3 10
5 10
5 10
4 12
输出

22

[分析]
01背包
但是背包容量超大,使用int来dp超出了内存范围。
这种时候肯定找更小的数字来dp。
思路和01背包一样,不同的是普通01背包dp的是背包容量
这一题dp价值总和。

[代码]

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
using namespace std;
#define max(a,b)(a>b?a:b)
#define min(a,b)(a<b?a:b)

typedef long long ll;
ll dp[10005], w[10005];
ll v[10005];

int main()
{
    int t, i, n, sum;
    ll V;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%lld", &n, &V);
        sum = 0;
        for (i = 1; i <= n; i++)
        {
            scanf("%I64d%d", &w[i], &v[i]);
            sum = sum + v[i];
        }

        memset(dp, 1000000010, sizeof(dp));
        dp[0] = 0;
        for (i = 1; i <= n; i++)
        {
            for (int j = sum; j >= v[i]; j--)
                dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
        }

        for (i = sum; i >= 0; i--)
        {
            if (dp[i] <= V)
            {
                printf("%d\n", i);
                break;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值