关闭

训练3 习题24

标签: acm
100人阅读 评论(0) 收藏 举报
分类:

题目:


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 10 10 15 10 5 10 5 3 10 5 10 5 3 5 6 2 7 3
 

Sample Output
5 11
 


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

#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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5905次
    • 积分:440
    • 等级:
    • 排名:千里之外
    • 原创:40篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    最新评论