HDU - 3466

01背包简单处理

题目进站口

原文:

Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more.
The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi.
If he had M units of money, what’s the maximum value iSea could get?

输入:

There are several test cases in the input.

Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money.
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description.

The input terminates by end of file marker.

输出:

For each test case, output one integer, indicating maximum value iSea could get.

#include<string>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
const int N = 1010;
typedef long long LL ;
//如果去掉q的约束,其实就是一道简单的01背包,因此就是如何处理p的约束
//在这里,可以知道01背包就是用到了上一个物品的体积进行后续的操作
//因此上面物品体积一定要小于我们现在计算的物品
//如果我们现在计算的物品体积小于之前的某一个物品体积
//就一定会发生我们所计算的f[i]=0,比如说我们之前是5 9 3 而现在是5 5 3
//之前我们计算出了f[9]但是对于f[8],f[7]我们是初值0 因此我们需要置换顺序
struct node
{
    int v,q,w;
} a[5010];
LL f[5010];
bool cmp(struct node a,struct node b)
{
    return a.q-a.v<b.q-b.v;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        memset(f,0,sizeof f);

        for(int i=1;i<=n;i++)   cin>>a[i].v>>a[i].q>>a[i].w;
        sort(a+1,a+n+1,cmp);
        
        for(int i=1;i<=n;i++)
            for(int j=m;j>=max(a[i].v,a[i].q);j--)
                f[j]=max(f[j],f[j-a[i].v]+a[i].w);
       
        cout<<f[m]<<endl;
    }
       
}

·

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pig2687

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值