Proud Merchants (01 backpack)

Source: Internet
Author: User

Proud merchantstime limit:2000/1000ms (java/other) Memory limit:131072/65536k (Java/other) total submission (s): 12 Accepted Submission (s): 5Problem descriptionrecently, 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?

Inputthere 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 I s in the description. The input terminates by end of file marker.

Outputfor each test case, output one integer, indicating maximum value ISea could get.

Sample Input2 10 10 15 10 5 10 5 3 10 5 10 5 3 5 6 2 7 3

Sample Output5 11 Code: Oneself originally thought is quite right, the more think more complex, in fact a sort; test instructions means to buy things more restrictions, limit how much money you must have to buy;

< Span style= "FONT-FAMILY:SIMSUN; Color: #333333; font-size:18px; " > The problem-solving idea : 01 Backpacks related to order. At first glance seems to be similar to the ordinary backpack, the capacity of more than Q only installed. But this will be a big problem, if an item p = 5,q = 7, an item p = 5,q = 9, if first, then only 7,8...M can carry on the state transfer, the second item when the 9,10..M to transfer, the second item transfer can borrow the first item of those States, The second item is shifted first, and the first re-transfer is not. Of course, there are prices related, when the limit of the same price is not in the same order will affect the results. A combination of sorting strategies-limited to a small price and expensive first choice, that is q-p small first choice. Why is that? A:P1,Q1 b:p2,q2, first select a, you need at least p1+q2 capacity, and first select B will need at least p2+q1, if p1+q2>p2+q1, then to choose two words must first select a then B, the formula can be replaced Q1-P1 < Q2-P2, Sorting the final order in this way is the optimal order.

The problem is to ensure that p[i]-q[i] small is "selected", the smaller the difference, the greater the value of its use (the smaller the sacrifices made).

Code:

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 #defineMAX (x, y) (x>y?x:y)5 Const intmaxn=5010;6 Const intmaxm=510;7 structnode{8     intp,q,v;9 };Ten intcmpConst void*a,Const void*b) { One     if((* (node *) a). Q (* (node *) a) .p< (* (node *) b). Q (* (node *) b). P)return-1; A     Else return 1; - } - Node DT[MAXM]; the intBAG[MAXN]; - intMain () { -     intn,m; -      while(~SCANF ("%d%d",&n,&M)) { +memset (Bag,0,sizeof(bag)); -          for(intI=0; i<n;i++) scanf ("%d%d%d",&dt[i].p,&dt[i].q,&dt[i].v); +Qsort (Dt,n,sizeof(dt[0]), CMP); A          for(intI=0; i<n;i++) at              for(intj=m;j>=dt[i].q;j--) {// -                     if(J&GT;=DT[I].P) {// -Bag[j]=max (bag[j],bag[j-dt[i].p]+dt[i].v); -                     } -             } -printf"%d\n", Bag[m]); in     } -     return 0; to}

Proud Merchants (01 backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.