HDU 3466 Proud Merchants (背包问题)
2013-08-20 23:55
148 查看
Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1899 Accepted Submission(s): 762
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output one integer, indicating maximum value iSea could get.
[align=left]Sample Input[/align]
2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3
[align=left]Sample Output[/align]
5
11
题意:商品的价格是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 ; 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; } } }
相关文章推荐
- 两道需要排序的dp背包下问题 -- hdu 2546 饭卡/ hdu 3466 Proud Merchants
- hdu 3466 Proud Merchants(背包问题)
- HDU 3466 Proud Merchants (01背包排序问题)
- HDU-3466 Proud Merchants (和放入顺序有关的背包问题 经典题)
- 背包问题
- nyoj 106 背包问题 【背包问题】【贪心】
- hdu-2191-多重背包问题----悼念512汶川大地震遇难同胞――珍惜现在,感恩生活
- 0-1背包问题
- 背包问题knapsack与动态编程
- 【算法】用“0-1背包问题”的观点来解搜狐2012年的那道题
- [DP] 51Nod 1597 有限背包计数问题
- 背包问题
- poj 1384 Piggy-Bank 多重背包问题
- hdu-1712 分组背包问题
- 0/1背包问题动态规划 空间复杂度是o(C)
- 动态规划0/1背包问题
- 0-1背包问题
- hdu1712 分组背包问题
- 0/1背包问题 - 动态规划(C++实现)
- 背包问题