设为首页 加入收藏

TOP

hdu 3466 Proud Merchants(01背包)
2016-01-29 16:36:27 】 浏览:203
Tags:hdu 3466 Proud Merchants 背包

题意:

给出

商品个数,拥有的金钱;

出售价格,最低金钱,实际价值;

求出能够获得的最大价值。

注意:

1)对Qi-Pi排序(差值)

java代码:

import java.util.Scanner;

public class Main {
	/*
	 * dp-表示背包数组,
	 */
	static int n, m, a, b, c;
	static int[] dp = new int[5001];
	static Node1171[] nodes = new Node1171[501];

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i < nodes.length; i++) {
			nodes[i] = new Node1171();
		}
		while (sc.hasNext()) {
			n = sc.nextInt();
			m = sc.nextInt();
			for (int i = 1; i <= n; i++) {
				a = sc.nextInt();
				b = sc.nextInt();
				c = sc.nextInt();
				nodes[i].set(a, b, c);
			}
			for (int i = 0; i <= m; i++) {
				dp[i] = 0;
			}
			mySort();
			for (int i = 1; i <= n; i++) {
				/*
				 * m为最大的重量,
				 * dp数组保存价值。
				 */
				for (int j = m; j - nodes[i].q >= 0; j--) {
					dp[j] = max(dp[j], dp[j - nodes[i].p] + nodes[i].v);
				}
			}
			System.out.println(dp[m]);
		}
	}

	public static int max(int a, int b) {
		return a > b  a : b;
	}

	/*
	 * 采用普通排序
	 */
	public static void mySort() {
		Node1171 temp = null;
		for (int i = 1; i < n; i++) {
			for (int j = i + 1; j <= n; j++) {
				/*
				 * 差值最小的为最优
				 */
				if (nodes[i].q - nodes[i].p > nodes[j].q - nodes[j].p) {
					temp = nodes[i];
					nodes[i] = nodes[j];
					nodes[j] = temp;
				}
			}
		}
	}

}

class Node1171 {
	int p;
	int q;
	int v;

	public Node1171() {
	}

	public void set(int p, int q, int v) {
		this.p = p;
		this.q = q;
		this.v = v;
	}

}


Proud Merchants

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



Problem Description Recently, iSea went to an ancient country. 最近,iSea去了一个古老的国家。 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, 商人是最典型的,他们每个人只卖出了一件商品,价格是Pi, but they would refuse to make a trade with you if your money were less than Qi, 如果你的金额小于Qi,那么他们会拒绝和你做贸易,
and iSea eva luated every item a value Vi. iSea评估了每一件商品,价值为Vi。
If he had M units of money, what’s the maximum value iSea could get
如果他有M个单元的金钱,那么iSea 可以获得的最大价值是多少?

Input There are several test cases in the input.
输入语句将包含多组测试事件。
Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), 每一个测试事件的起始行包含两个整数N和M, 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), 接下来输入N行,每一行包含三个整数,Pi,Qi,Vi, 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.
对于每一个测试事件,输出一个整数,表示iSea 能够获得的最大的价值。
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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇hihoCoder - 1116 - 计算 (线段.. 下一篇STM32F4 输入输出(GPIO)模式理解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目