HDU--3466--Proud Merchants--01背包

Proud Merchants

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 2777    Accepted Submission(s): 1155

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

题意:给你N个物品和M单位的金钱,每个物品有价格a、购买的前提资金b、价值c,要买某个物品,你的钱必须大于等于b,求出最大能获得的价值量

这是个有前提的背包问题,主要是判断物品出场的顺序,本人表示跟上一题一样,准备作死的节奏,就不解析了,等弄明白了再加上

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max(a,b) a>b?a:b
using namespace std;
struct node
{
    int a,b,c;
}s[555];
bool cmp(const node &a,const node &b)
{
    return a.b-a.a<b.b-b.a;	//排序规则
}
int main (void)
{
    int n,m,i,j,k,l;
    int dp[5555];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d",&s[i].a,&s[i].b,&s[i].c);
        }
        sort(s,s+n,cmp);
        memset(dp,0,sizeof(dp));
        for(i=0;i<n;i++)	//01背包
        for(j=m;j>=s[i].b;j--)
        dp[j]=Max(dp[j],dp[j-s[i].a]+s[i].c);
        printf("%d\n",dp[m]);
    }
    return 0;
}

总结:今天就做了这两个题,一个是K大值的背包,一个是有前提的背包,这几天有得溜了!

时间: 2024-10-31 15:29:45

HDU--3466--Proud Merchants--01背包的相关文章

hdu 3466 Proud Merchants(0-1背包+排序)

题目来源:hdu 3466 Proud Merchants Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3595 Accepted Submission(s): 1500 Problem Description Recently, iSea went to an ancient country. For

hdu 3466 Proud Merchants(有排序的01背包)

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4039    Accepted Submission(s): 1677 Problem Description Recently, iSea went to an ancient country. For such a long time, it was

HDU 3466 Proud Merchants(01背包)

这道题目看出背包很容易,主要是处理背包的时候需要按照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 2674    Accepted Submission(s): 1109 Problem Description Recently, iSea we

hdu 3466 Proud Merchants 自豪的商人(01背包,微变形)

题意: 要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖.给出身上的钱和所有东西的3个属性,求最大总价值. 思路: 1)WA思路:与01背包差不多,dp过程中记录每个容量所能获得的最大价值以及剩余的容量.实现是,开个二维dp数组,从左往右扫,考虑背包容量为j的可获得价值量,根据该剩余容量得知要更新后面哪个背包容量[j+?] ,如果剩余容量大于q[i]那么可以直接装进去,更新价值以及剩余容量,否则,考虑更新的是更大的背包容量.这个思路有缺陷,一直找

hdu 3466 Proud Merchants &lt;背包+sort排序&gt;

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3616    Accepted Submission(s): 1511 Problem Description Recently, iSea went to an ancient country. For such a long time, it was

Proud Merchants(01背包)

Proud Merchants Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 12   Accepted Submission(s) : 5 Problem Description Recently, iSea went to an ancient country. For such a long time, it was the mo

HDU 3466 Proud Merchants(0-1背包)

http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意: 最近,iSea去了一个古老的国家.在这么长的时间里,它是世界上最富有和最强大的王国.结果,这个国家的人民仍然非常自豪,即使他们的国家没有那么富有了.商人是最典型的,每个人只卖一个项目,价格是Pi,但如果你的钱少于Qi,他们会拒绝与你交易,iSea评估每个项目一个值Vi.如果他有M单位的钱,iSea可以获得的最大价值是多少? 思路: 这道题的话多加了一个Qi. 一定要注意,若要保证动归方程无后效性

hdu 3466 Proud Merchants 【限制性01背包】

题目链接:https://vjudge.net/contest/103424#problem/J 转载于:https://www.bbsmax.com/A/RnJW16GRdq/ 题目大意: 有n个商品m块钱,给出买每个商品所花费的钱P.钱包里需要至少Q才有资格买.商品的价值V.问你如何购买商品的价值最大. 解题分析:考虑下面的例子:A:p1=5, q1=10, v1=5; B:p2=3, q2=5, v2=6; 如果先买物品A再买物品B的话我至少需要10元钱,也就是money >= p1+q2

HDU 2602 (简单的01背包) Bone Collector

很标准的01背包问题 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1000 + 10; 8 int w[maxn], v[maxn], dp[maxn]; 9 10 int main(void) 11 { 12 #ifdef LOCAL 13 freopen(&qu

HDU 1171 Big Event in HDU(母函数或01背包)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 44151    Accepted Submission(s): 15191 Problem Description Nowadays, we all know that Computer College is the biggest department