您的位置:首页 > 其它

hdu 3466 Proud Merchants(贪心+背包)

2015-03-31 16:36 239 查看

Proud Merchants

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 3101 Accepted Submission(s): 1282



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
题目分析:是一个背包和搬家问题的结合,背包物品放入没有顺序,但是搬家问题有顺序,所以要按搬家问题先排序,然后进行0/1背包

那么难点在于对搬家问题的理解,如何进行排序呢?那么我们先假设有两件物品i,j,ai,aj代表放入i,j的最小花费,bi,bj代表放入i,j的最小空间需求,那么我们如果先放入i的话,最少需要ai+bj的空间,先放入j的话,需要aj+bi的空间,那么如果ai+bj<aj+bi,可以变形为ai-bi<aj-bj我们一定先放入i,但是背包式倒序放入,所以排序是按照bi-ai排序,然后0/1背包就很简单了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 507
#define M 5007

using namespace std;

int n,m;
int dp[M];

struct A
{
    int p,q,v;
    bool operator < (const A& a ) const
    {
        return q-p < a.q-a.p;
    }
}a[M];

int main ( )
{
    while (~scanf ( "%d%d" , &n , &m ) )
    {
        for ( int i = 0 ; i < n ; i++ )
            scanf ( "%d%d%d" , &a[i].p , &a[i].q , &a[i].v );
        sort ( a , a+n );
        memset ( dp , 0 , sizeof ( dp ));
        for ( int i = 0 ; i < n ; i++ )
            for ( int j = m ; j >= a[i].p && j >= a[i].q ; j-- )
                dp[j] = max ( dp[j] , dp[j-a[i].p] + a[i].v );
        printf ( "%d\n" , dp[m] );
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: