首页 > 代码库 > HDU3466 Proud Merchants[背包DP 条件限制]
HDU3466 Proud Merchants[背包DP 条件限制]
Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 5599 Accepted Submission(s): 2362
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?
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.
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
Author
iSea @ WHU
Source
2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU
题意:n件物品有m元,物品价格p,价值q,条件是手里多于q元才能买,求最大价值
按q-p小到大排序然后01背包
f[i][j]是前i个物品j元,只有j>qi时才能买第i个物品
所以考虑第i个物品,普通情况下最小可以从f[0]更新来,现在是f[qi-pi],也就是说qi-pi被浪费了
这个浪费当然越小越好
A:p1,q1 B: p2,q2,先选A,则至少需要p1+q2的容量,而先选B则至少需要p2+q1,如果p1+q2>p2+q1,那么要选两个的话的就要先选A再选B,公式可换成q1-p1 < q2-p2,
首先发现qi-pi小,不可能有比qi-pi更小的qj,使得可以从j到i
这样可以保证后来的j-pi大于前面的q-p,也就是更新来的范围递增
就按照取出两个比较的套路来行了
//// main.cpp// hdu3466//// Created by Candy on 29/10/2016.// Copyright © 2016 Candy. All rights reserved.//#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;const int N=505,M=5005;inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} return x*f;}int n,m;struct item{ int p,q,w; bool operator <(const item &r)const{return q-p<r.q-r.p;}}a[N];int f[M];void dp(){ memset(f,0,sizeof(f)); for(int i=1;i<=n;i++) for(int j=m;j>=a[i].q;j--) f[j]=max(f[j],f[j-a[i].p]+a[i].w);}int main(int argc, const char * argv[]){ while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++){a[i].p=read();a[i].q=read();a[i].w=read();} sort(a+1,a+1+n); dp(); printf("%d\n",f[m]); } return 0;}
HDU3466 Proud Merchants[背包DP 条件限制]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。