hdu 3466 Proud Merchants (01背包 + 结构体的sort排序)

本文介绍了一种在01背包问题基础上增加特定条件约束的优化算法。通过排序和调整购买顺序来实现最大价值购买的问题解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Proud Merchants

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7069    Accepted Submission(s): 2944


Problem Description
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?

 

Input
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.

 

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

 

Sample Input
  
  
2 10 10 15 10 5 10 5 3 10 5 10 5 3 5 6 2 7 3
 

Sample Output
  
  
5 11
 

Author
iSea @ WHU
 

Source
 

Recommend
zhouzeyong


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3466

题目大意:有n件商品和购买余额m,每件商品包含如下信息:商品的价格p,允许购买该商品的最少余额q,以及商品的价值v。求用余额m最多能购买到的商品价值。

解题思路:
        这题在01背包的基础上多了一个限制商品购买的条件。普通的01背包考虑的是每一件商品买或者不买,和购买顺序无关,但这题加了条件限制后,先买后买对是否能买一件商品就有很关键的影响了。
       举个例子:A商品:p1 = 1,q1 = 3 ,v1 = 1;
 B商品:p2 = 5,q2 = 5 , v2 = 1;
       如果这时候余额为6,先买A商品后,余额为5,可以继续购买B商品,两件商品都能购买。如果先买B商品,余额为1,小于q1,不能购买A商品。即若要购买两件商品,先买A商品时需满足m >= p1 + q2 = 6, 先买B商品时需满足m >= p2 + q1 = 8,明显先买A商品需要的余额最少,即p1 + q2 < p2 + q1 时 应先买p1对应的商品,不等式转换一下为:p1 - q1 < p2 - q2,即 p 和 q 差值小的商品应先购买。但要注意!01背包的操作顺序是从后往前购买的,即最后一个商品都是确定购买的,因此sort排序时把p 和 q 差值小的商品放在后面。
       在输入商品信息时纪录p - q值,然后对其sort从大到小排序,按此顺序进行01背包计算即可。

代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 5005;
int dp[maxn];

struct product{
    int q,p,v;
    int d;
}pro[505];

bool cmp(product x,product y){
    return x.d > y.d; 
}

int main(void){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(dp,0,sizeof(dp));
        for(int i = 0;i < n; i ++){
            scanf("%d%d%d",&pro[i].p,&pro[i].q,&pro[i].v);
            pro[i].d = pro[i].p - pro[i].q; 
        }
        sort(pro,pro + n,cmp);
        int cur = m;
        for(int i = 0 ;i < n ;i ++ ){
            for(int j = m;j >= pro[i].p; j--){
                if(j >= pro[i].q)      
                    dp[j] = max(dp[j],dp[j - pro[i].p] + pro[i].v );
                else
                    break;
            }
        }
        printf("%d\n",dp[m]);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值