Proud Merchants HDU - 3466 (思路题--有排序的01背包)
2017-03-24 20:07
323 查看
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
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; }
相关文章推荐
- hdu3466——Proud Merchants——————【01背包排序】
- 【背包】HDU 3466 Proud Merchants DP 分享排序方式的证明。。
- HDU 3466 Proud Merchants (需要排序的01背包)
- Proud Merchants HDU - 3466 dp,贪心,还是不太理解
- HDU 3466 Proud Merchants(01背包/按limit-price从小到大排序)
- hdu 3466 Proud Merchants <背包+sort排序>
- HDU 3466 Proud Merchants(排序的01背包)@
- hdu 3466 Proud Merchants(先排序的01背包)
- 【HDU - 3466 】Proud Merchants 【排序+01背包】
- HDU_3466_Proud Merchants详细解答(01背包(排序问题))
- hdu 3466 Pround Merchants 排序+01背包
- Proud Merchants HDU - 3466
- Proud Merchants HDU - 3466
- HDU Proud Merchants 3466 0-1背包变形
- HDU 3466 Proud Merchantss
- HDU Proud Merchants 【需要排序的0/1背包问题】
- hdu 3466 Proud Merchants(有排序的01背包)
- Proud Merchants HDU - 3466 贪心背包的证明
- hdu Proud Merchants
- hdu 3466 Proud Merchants(0-1背包+排序)