动态规划 HDOJ-1114 完全背包

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114

1.问题描述

给出每种硬币的重量与价值,计算储蓄罐中硬币重量为x时,所含硬币的最少总价。每种硬币有无限个。

2.输入

T
E F
N
P1 W1
P2 W2
...
Pn Wn

T:测试用例个数
E:空猪储蓄罐的重量
F:硬币+储蓄罐的重量
N:硬币种类数
Pi :第i种硬币的价值
Wi:第i种硬币的重量
1 <= E <= F <= 10000

3.输出

输出储蓄罐中硬币的最少价值——“ The minimum amount of money in the piggy-bank is XX.
若无论怎样装硬币都不能让总重恰好为规定的值,则输出“ This is impossible.”。

4.示例输入

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

5.示例输出

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

6.分析

完全背包。求总重恰好为x时的最少价值。

7.代码

7.1 一维dp[]

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
	static Scanner scanner;
	static boolean debug = true;
	int totalWeight;
	int coinKind;
	int w[]=new int[500],v[]=new int[500];
	int []dp;
	void read() {
		totalWeight=(scanner.nextInt()-scanner.nextInt())*-1;
		coinKind=scanner.nextInt();
		for(int i=0;i
    
    
     
     < 0?Integer.MAX_VALUE:temp );
				
	}
	int dp(int j){
		if(j==0)
			return 0;
		if(j<0)
			return Integer.MAX_VALUE;
		return dp[j];
	}
	void output() {
		int answer;
		if((answer=dp[totalWeight])==Integer.MAX_VALUE)
			System.out.println("This is impossible.");
		else
			System.out.println("The minimum amount of money in the piggy-bank is "+answer+".");
	}

	public static void main(String[] args) throws FileNotFoundException {
		scanner = new Scanner(System.in);
		if (debug)
			scanner = new Scanner(new File("d:/in.txt"));
		Main obj = new Main();
		int ncase = Integer.valueOf(scanner.nextLine());
		while (ncase-->0) {
			obj.read();
			obj.calc();
			obj.output();
		}
	}



}
    
    

7.2二维dp[][]

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
	static Scanner scanner;
	static boolean debug = true;
	int totalWeight;
	int coinKind;
	int w[] = new int[500], v[] = new int[500];
	int[][] dp;

	void read() {
		totalWeight = (scanner.nextInt() - scanner.nextInt()) * -1;
		coinKind = scanner.nextInt();
		for (int i = 0; i < coinKind; i++) {
			v[i] = scanner.nextInt();
			w[i] = scanner.nextInt();
		}
		dp = new int[coinKind][totalWeight + 1];
		for (int i = 0; i < coinKind; i++)
			for (int j = 0; j <= totalWeight; j++)
				dp[i][j] = Integer.MAX_VALUE;
	}

	void calc() {
		int temp = 0;
		for (int i = 0; i < coinKind; i++)
			for (int j = 0; j <= totalWeight; j++)
				dp[i][j] = Math.min(dp(i - 1, j), (temp = dp(i, j - w[i]) + v[i]) < 0 ? Integer.MAX_VALUE : temp);
	}

	int dp(int i, int j) {
		if (i == 0&& j==0)
			return 0;
		if(i<0 ||j<0)
			return Integer.MAX_VALUE;
		return dp[i][j];
	}

	void output() {
		int answer;
		if ((answer = dp[coinKind - 1][totalWeight]) == Integer.MAX_VALUE)
			System.out.println("This is impossible.");
		else
			System.out
					.println("The minimum amount of money in the piggy-bank is "
							+ answer + ".");
	}

	public static void main(String[] args) throws FileNotFoundException {
		scanner = new Scanner(System.in);
		if (debug)
			scanner = new Scanner(new File("d:/in.txt"));
		Main obj = new Main();
		int ncase = Integer.valueOf(scanner.nextLine());
		while (ncase-- > 0) {
			obj.read();
			obj.calc();
			obj.output();
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值