如题: http://acm.hdu.edu.cn/showproblem.php?pid=3466
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?
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.
2 10 10 15 10 5 10 5 3 10 5 10 5 3 5 6 2 7 3
5 11
比一般背包多了一个变量Qi,当钱数大于Qi时,才能买i组的物品
于是按照Qi-Ci从小到大排序,差值越小越能选,然后背包
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define max(a,b)(a>b?a:b)
struct good
{
int c,Q,w;
}goods[510];
int f[5010];
int cmp(good &a,good &b)
{
return a.Q-a.c<b.Q-b.c;
}
int main()
{
int N,M;
// freopen("C:\\1.txt","r",stdin);
while(~scanf("%d %d",&N,&M))
{
memset(goods,0,sizeof(goods));
memset(f,0,sizeof(f));
int i,j;
for(i=1;i<=N;i++)
scanf("%d%d%d",&goods[i].c,&goods[i].Q,&goods[i].w);
sort(goods+1,goods+1+N,cmp);
for(i=1;i<=N;i++)
for(j=M;j>=goods[i].Q;j--)
f[j]=max(f[j],f[j-goods[i].c]+goods[i].w);
printf("%d\n",f[M]);
}
return 0;
}