HDU 3466 Proud Merchants (背包問題)

來源:互聯網
上載者:User
Proud Merchants

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1899    Accepted Submission(s): 762

Problem DescriptionRecently, 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?

 

InputThere 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.

 

OutputFor 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
 

 

題意:商品的價格是Pi, 你的錢要大於等於Qi才可以購買,購買商品所或得的價值Vi

背包問題:公式 dp[j]=dp[j]=Math.max(dp[j],dp[j-k1]+k2);

 

import java.io.*;import java.util.*;public class Main {int n,m,M=6000;int dp[]=new int[M];public static void main(String[] args) {new Main().work();}void work(){Scanner sc=new Scanner(new BufferedInputStream(System.in));while(sc.hasNext()){n=sc.nextInt();m=sc.nextInt();Node node[]=new Node[n];for(int i=0;i<n;i++){int a=sc.nextInt();int b=sc.nextInt();int c=sc.nextInt();node[i]=new Node(a,b,c);}Arrays.sort(node);//每件商品按 (this.qi-this.pi)>(o.qi-o.pi) 進行升序排序Arrays.fill(dp,0);for(int i=1;i<=n;i++){for(int j=m;j>=0;j--){if(j>=node[i-1].qi){int k1=node[i-1].pi;int k2=node[i-1].vi;dp[j]=Math.max(dp[j],dp[j-k1]+k2);}}}System.out.println(dp[m]);}}class Node implements Comparable<Node>{int pi;int qi;int vi;Node(int pi,int qi,int vi){this.pi=pi;this.qi=qi;this.vi=vi;}public int compareTo(Node o) {return (this.qi-this.pi)>(o.qi-o.pi)?1:-1;}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.