训练3 习题24

来源:互联网 发布:暗影精灵3 新机优化 编辑:程序博客网 时间:2024/05/18 02:44

题目:


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.<br>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.<br>If he had M units of money, what’s the maximum value iSea could get?<br><br>
 

Input
There are several test cases in the input.<br><br>Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money.<br>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.<br><br>The input terminates by end of file marker.<br><br>
 

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

Sample Input
2 1010 15 105 10 53 105 10 53 5 62 7 3
 

Sample Output
511
 


思路:本来是简单的背包问题  不过有价格限制  需要满足 价格 差值越小使用它的价值越大(所以先排序)

#include<cstring>  
#include<cstdio>  
//#include<algorithm>  
struct things{  
 int p,q,v;  
}thing[510];  
bool cmp(things a,things b){  
 return (a.q-a.p)<(b.q-b.p);  
}  
using namespace std;


int max(int a1,int a2)
{


if(a1>a2)
return a1;
return a2;
}
int main()  
{  
    int n,maxv,i,j,dp[5010];  
    while(scanf("%d%d",&n,&maxv)!=EOF)  
    {  
        memset(dp,0,sizeof(dp));  
        for(i=0;i<n;i++)  
        {  
            scanf("%d%d%d",&thing[i].p,&thing[i].q,&thing[i].v);  
        }  
        sort(thing,thing+n,cmp);  
        for(i=0;i<n;i++)  
        {  
          for(j=maxv;j>=thing[i].q;j--)  
           {  
  
             dp[j]=max(dp[j],dp[j-thing[i].p]+thing[i].v);  
           }  
        }  
        printf("%d\n",dp[maxv]);  
    }  
    return 0;  
}  

0 0
原创粉丝点击