Proud Merchants HDU

来源:互联网 发布:手机音乐合成剪辑软件 编辑:程序博客网 时间:2024/06/08 19:10

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

//很明显这一道题是用01背包写,但是又和普通的01背包不一样,因为这一道题多了一个q的限制//如果一个物品是5 9,一个物品是5 6,//对第一个进行背包的时候只有dp[9],dp[10],…,dp[m],//再对第二个进行背包的时候,如果是普通的,应该会借用前面的dp[8],dp[7]之类的,//但是现在这些值都是0,所以会导致结果出错.//(重点):于是要想到只有***后面要用的值前面都可以得到***,那么才不会出错//也就是说有两个物品A(p1,q1)和B(p2,q2)如果你想进行背包,则在这一题中必须要满足把p1实际的大小装下//还要满足剩下的空间够把q2装下,在进行背包的顺序中先A后B为: p1+q2,先B后A为: p2+q1,//则p1+q2>p2+q1,即q1-p1<q2-p2#include<map>#include<queue>#include<stack>#include<vector>#include<math.h>#include<cstdio>#include<sstream>#include<numeric>//STL数值算法头文件#include<stdlib.h>#include<string.h>#include<iostream>#include<algorithm>#include<functional>//模板类头文件using namespace std;const int INF=1e9+7;const int maxn=5100;typedef long long ll;int n,m;int dp[maxn];struct node{    int p,q,v,t;} a[maxn];int cmp(node A,node B){    return A.t<B.t;}int main(){    while(scanf("%d %d",&n,&m)!=EOF)    {        int i,j;        memset(a,0,sizeof(a));        memset(dp,0,sizeof(dp));        for(i=0; i<n; i++)        {            scanf("%d %d %d",&a[i].p,&a[i].q,&a[i].v);            a[i].t=a[i].q-a[i].p;        }        sort(a,a+n,cmp);        for(i=0; i<n; i++)        {            for(j=m; j>=a[i].q; j--)            {                dp[j]=max(dp[j],dp[j-a[i].p]+a[i].v);            }        }        printf("%d\n",dp[m]);    }    return 0;}
0 0