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

2010 ACM-ICPC Multi-University
Training Contest(3)——Host by WHU

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

///#define mod 10007

const int maxn = 5010;
using namespace std;

int dp[maxn];

struct node
{
    int p, q, v;
}f[510];

bool cmp(node a, node b)
{
    return a.q-a.p < b.q-b.p;
}

int main()
{
    int n, m;
    while(~scanf("%d %d",&n, &m))
    {
        for(int i = 1; i <= n; i++) scanf("%d %d %d",&f[i].p, &f[i].q, &f[i].v);
        for(int i = 0; i <= m; i++) dp[i] = 0;
        sort(f+1, f+n+1, cmp);
        for(int i = 1; i <= n; i++)
            for(int j = m; j >= f[i].q; j--) dp[j] = max(dp[j] , dp[j-f[i].p] + f[i].v);
        printf("%d\n",dp[m]);
    }
}

/*
3 10
3 6 10
3 8 4
2 10 7
*/
时间: 2024-11-17 22:58:34

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背包,微变形)

题意: 要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖.给出身上的钱和所有东西的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