hdoj 3466——骄傲的商人——01背包

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

这里比01背包增加了一个条件(当你的钱大于Q时才有可能购买)

细看代码,现在还迷迷糊糊,(代码是人家的)

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<list>
#include<iterator>
#include<stack>
#include <queue>
#include <cstdio>
#include<algorithm>
using namespace std;
typedef  long long ll;
typedef unsigned long long ull;
#define e 2.718281828459
#define INF 0x7fffffff
#pragma warning(disable:4996)
#define sf scanf
#define pf printf
#define sf2d(x,y) scanf("%d %d",&(x),&(y))
#define sfd(x) scanf("%d",&x)
#define sff(p) scanf("%lf",&p)
#define pfd(x) printf("%d\n",x)
#define mset(x,b) memset((x),b,sizeof(x))
const double pi = acos(-1.0);
const long long mod = 1000000007LL;


using namespace std;
struct node {
    int p;//重量,价格
    int v;//价值
    int q;//基础

    friend bool operator<(node a, node b) {
        return a.q - a.p<b.q - b.p;
    }

}s[505];

int dp[5005];
int main(void) {                                       //假如s[a].p+s[b].p<=m则这两个物品都应该被
    int n, m, i, j;                                    //装入背包,但现在有了限制之后物品的重量可能
    while (scanf("%d%d", &n, &m) != EOF) {             //就被当做s[a].q和s[b].q,也就有可能这两个物品
        for (i = 1; i <= n; i++)                //就不能装入这个背包中,所以放入背包的顺序也对结果
            scanf("%d%d%d", &s[i].p, &s[i].q, &s[i].v);//造成了影响
        mset(dp,0); 
        sort(s + 1, s + n + 1);                        //当s[a].q<=s[a].p时跟普通01背包没有区别也就没有了
        for (i = 1; i <= n; i++) {                      //限制条件,所以关键在于s[a].q>s[a].p时,为了使转移
            for (j = m; j >= max(s[i].q, s[i].p); j--)  //到下个物品时,转移的范围最大,因此下限小应该先放入
                dp[j] = max(dp[j], dp[j - s[i].p] + s[i].v);
        }                                       //背包,也就是按照s[a].q-s[a].p排序
        printf("%d\n", dp[m]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jiruqianlong123/article/details/82527359
今日推荐