HDU 3466 Proud Merchants

来源:互联网 发布:s7总决赛数据统计 编辑:程序博客网 时间:2024/05/04 04:42

Proud Merchants

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


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 1010 15 105 10 53 105 10 53 5 62 7 3
 

Sample Output
511
 
题意:
有N个物品,价格为p,但你想买的话手里的钱不能低于q,价值为w。给你M块钱,求得到的最大价值。
分析:
若没有限制条件的话,01背包很好解决,就是这个条件让这个问题很棘手!
做了两天,搜了很多别人的题解,下面是个人的一点理解
要想在限制条件下获得最大价值,要满足买了一件物品后尽量使剩下的钱最多,这样尽量满足下一个物品的限制条件!
假如有两个商品:
物品   价格       限制条件
  A       p1             q1
  B       p2             q2
1:先买A,至少有 p1 + q2 元钱;先买B,至少有 p2 + q1 元钱。
2:假设先买A需要的钱少,即 p1 + q2 < p2 + q1,也就是 q1 - p1 > q2 - p2。
3:所以,先买 q - p 大的!需要将这些物品按 q - p 升序排列。
4:咦,有人该问了,不是先买差值大的,那怎么升序排列了?这样不就是先买差值小的了!
5:这个我刚开始也是困惑,下面慢慢分析:
看动态转移方程:dp[ j ] = max(dp[ j ], dp[ j - v[ i ] ] + w[ i ]),计算 j 的时候已经将 j - v[ i ] 算出
就是买第一个的时候已经决定了第二个是否买了。这个要自己有耐心地慢慢体会。。。。。。
还有大神说要满足无后效性:j - v[ i ] 定比 j 先算出来,对物品 i 最小能算到q[ i ] - p[ i ],因此以q[ i ] - p[ i ] 排序。个人感觉与上面的分析有异曲同工之妙。
不多废话,看代码:
#include<stdio.h>#include<iostream>#include<cstring>#include<algorithm>using namespace std;struct wu{    int v1, v2, w;} W[505];int dp[5500];bool cmp(wu a, wu b){    return a.v2 - a.v1 < b.v2 - b.v1;}int main(){   // textbackground(2);    int n, m;    while(~scanf("%d%d", &n, &m)){        memset(dp, 0, sizeof(dp));        for(int i = 1; i <= n; i++) scanf("%d%d%d", &W[i].v1, &W[i].v2, &W[i].w);        sort(W + 1, W + n + 1, cmp);        for(int i = 1; i <= n; i++)            for(int j = m; j >= W[i].v2; j--)                dp[j] = max(dp[j], dp[j - W[i].v1] + W[i].w);        cout <<dp[m] <<endl;    }    return 0;}



0 0
原创粉丝点击