HDU 3466 Proud Merchants

Proud Merchants

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on HDU. Original ID: 3466
64-bit integer IO format: %I64d      Java class name: Main
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

Source

 
解题:陈国林的CSDN博客中是这样说的:
for (i=1; i<=n; i++)
    for (j=m; j>=q[i]; j--)
    dp[j]=max(dp[j],dp[j-p[i]]+v[i]);
要保证dp方程无后效性 j-p[i]一定要比j先算,那么当算i时,最小能算到q[i]-p[i],这样保证后面的可以用到前面的状态,因此以q[i]-p[i]排序即可保证无后效性。
 
这种说法比较好理解的,确实!
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 505;
 4 int N,M,dp[5010];
 5 struct node{
 6     int P,Q,V;
 7     bool operator<(const node &t) const{
 8         return Q - P < t.Q - t.P;
 9     }
10 }d[maxn];
11 int main() {
12     while(~scanf("%d %d",&N,&M)) {
13         memset(dp,0,sizeof dp);
14         for(int i = 0; i < N; ++i)
15             scanf("%d %d %d",&d[i].P,&d[i].Q,&d[i].V);
16         sort(d,d+N);
17         for(int i = 0; i < N; ++i) {
18             for(int j = M; j >= d[i].Q ; --j)
19                 dp[j] = max(dp[j],dp[j - d[i].P] + d[i].V);
20         }
21         printf("%d
",dp[M]);
22     }
23     return 0;
24 }
25 /*
26 3 10
27 5 10 5
28 2 7 3
29 3 5 6
30 */
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4417974.html