题目链接:传送门 题面:
Proud Merchants
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
题目大意:
输入有多组数据。现在有个物品元钱,每个物品有三个参数,分别是价格,买这个物品至少拥有的钱数,价值。你要用元钱买到价值尽量高的物品。
这题就比较巧妙了
多了一个参数
其实正解很简单
根据从小到大排序就好了
那为什么是这样的呢?
假设如果一个物品是
一个物品是
对第一个进行背包的时候只有
再对第二个进行背包的时候
会借用前面的到
但是现在这些值都是0
会导致结果出错
于是要想到只有后面要用的值前面都可以得到
那样才不会出错
设,
如果先后
则至少需要的容量
如果先后
至少需要的容量
那么就是
变形之后就是。
所以要针对每个属性的来进行排序
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
#define
#define
#define
using namespace std;
struct node {
int w, v, p;
friend bool operator < (const node a, const node b) {
return a.p - a.w < b.p - b.w;
}
} e[A];
int T, n, V, f[A];
int main() {
while (cin >> n >> V) {
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i++) cin >> e[i].w >> e[i].p >> e[i].v;
sort (e + 1, e + n + 1);
for (int i = 1; i <= n; i++)
for (int j = V; j >= e[i].p; j--)
f[j] = max(f[j], f[j - e[i].w] + e[i].v);
cout << f[V] << endl;
}
}