暑假集训第四周 DP C - Proud Merchants

来源:互联网 发布:分水岭算法示意图 编辑:程序博客网 时间:2024/06/07 03:04


C - Proud Merchants
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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 1010 15 105 10 53 105 10 53 5 62 7 3
 

Sample Output

511

就加了一个排序而已


1234567891011121314151617181920212223242526272829
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;struct bone{    int a,b,c;} v[501];int cmp( bone x,bone y){    return x.b-x.a<y.b-y.a;}int main(){    int t,n,m,s,i,j,k,f[5010];    while(scanf("%d %d",&t,&n)!=EOF)    {        memset(f,0,sizeof(f));        for(i=0; i<t; i++)            scanf("%d %d %d",&v[i].a,&v[i].b,&v[i].c);        sort(v,v+t,cmp);        for(i=0;i<t;i++)            for(j=n;j>=v[i].b;j--)            if(f[j]<f[j-v[i].a]+v[i].c)            f[j]=f[j-v[i].a]+v[i].c;        printf("%d\n",f[n]);    }    return 0;}

0 0
原创粉丝点击