Title Source: Hdu 3466 Proud Merchants
Proud Merchants
Time limit:2000/1000 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 3595 Accepted Submission (s): 1500
Problem Description
Recently, ISea went to an ancient country. For such a long time, it is the most wealthy and powerful kingdom in the world. As a result, the people in this country is still very proud even if their nation hasn ' t been so wealthy any more.
The merchants were the most typical, each of the them only sold exactly one item, the price is Pi, but they would refuse to M Ake a trade with the 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 is several test cases in the input.
Each test case begin with the integers N, M (1≤n≤500, 1≤m≤5000), indicating the items ' number and the initial mone Y.
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1≤pi≤qi≤100, 1≤vi≤1000), their meaning is I n 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
Main topic:
There are n goods, M yuan, each item p represents the sale price, q is a fixed price, only your balance is greater than or equal to the price of the product Q, you can buy the product, the value of the product is V, please buy the maximum value you can.
Topic Analysis:
Generally when the product contains the price and value (its own conditions more than two), it is necessary to sort the goods according to the price, and then use 0-1 backpack for the search, the title needs to order the product (Q-P), the purpose of this is to make the previous update of the value does not affect the status of the later update.
AC Code:
#include <stdio.h>#include <string.h>#include <algorithm>using namespace STD;structnode{//Use the structure to store the q,p,v of the product for easy sorting processing intP,q,v;}; Node a[10010];intf[10010];//Store the value of purchased goodsintCMP (node A,node b)//Sort the value of the item (Q-P) from small to large{return(A.Q-A.P) < (B.Q-B.P);}intMain () {intN,m; while(scanf("%d%d", &n,&m)!=eof) {memset(F,0,sizeof(f)); for(intI=0; i<n;i++) {scanf("%d%d%d", &A[I].P,&A[I].Q,&A[I].V); } sort (a,a+n,cmp); for(intI=0; i<n;i++)//0-1 Backpack Finder{ for(intj=m;j>=a[i].q;j--) {F[j]=max (F[J],F[J-A[I].P]+A[I].V); } }printf("%d\n", F[m]); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 3466 Proud Merchants (0-1 backpacks + sort)