HDU

来源:互联网 发布:淘宝开店卖景点门票 编辑:程序博客网 时间:2024/06/05 04:44

Proud Merchants

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7095 Accepted Submission(s): 2953

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

题意:给你一个n,m表示n个样例,m是总的背包容量,每组样例中给你pi,qi,vi,分别表示piqiqivi问你背包装满后的最大价值

分析: 第一次见到这个题我是直接按照价钱排序,从大到小,试了组样例不可以,有转换思路,多加了一维表示价格,又不行,最后看了kuangbin的思路,才恍然大悟,直接对q-p进行排序,然后01背包即可,这样理解,先是贪心的让你赔更少的钱,也就是先考虑这个差最少,然后再依次递推到大的就可以了

参考代码

#include <bits/stdc++.h>using namespace std;const int N = 1e5+10;struct node{    int q,p,v;    bool operator < (const node &x)const{ //重载小于        return (this->q-this->p) < (x.q-x.p);    }}a[N];int dp[N];int main(){    int n,m;    while(cin>>n>>m){        memset(dp,0,sizeof(dp));        for(int i = 0;i < n;i++){            cin>>a[i].p>>a[i].q>>a[i].v;        }        sort(a,a+n);        for(int i = 0;i < n;i++){            for(int j = m;j >= a[i].q;j--){                dp[j] = max(dp[j],dp[j-a[i].p]+a[i].v);            }        }        cout<<dp[m]<<endl;    }    return 0;}
  • 如有错误或遗漏,请私聊下UP,ths
原创粉丝点击