HDOJ.1114-Piggy-Bank

Topic Description:

    Before ACM can do anything, it must prepare budget and obtain necessary financial support. The main revenue from the move comes from irreversibly binding currencies (IBM). The idea behind it is simple. Whenever some ACM members have any small money, they throw all the coins into the piggy bank. You know that this process is irreversible. Without BR, coins can't be removed to steal pigs. After a long enough time, there should be enough cash in the piggy bank to cover all the expenses. But the piggy bank has a big problem. It's impossible to be sure how much money there is. So we may beat the pigs to pieces and find that we don't have enough money. Obviously, we want to avoid this unpleasant situation. The only possibility is that it weighs the piggy bank and tries to guess how many coins are in it. Suppose we can accurately determine the weight of a pig, and we know the weight currency of all coins given copper. Then, there are some minimum amounts in the deposit tank that we can guarantee. Your task is to find out the worst-case scenario and determine the minimum amount of cash in the savings tank. WW I need your help.

Input:

    Input consists of t test cases. Give their number on the first line of the input file. Each test case begins with a row containing two integers E and F. They represent an empty pig and a pig full of coins. Both weights are in grams. No pig will exceed 10 kg, which means 1 < = e < = f < = 10000. On the second line of each test case is an integer n (1 < = n < = 500), which gives the number of coins used in a given currency. Here are n lines, each of which specifies a coin type. These lines contain two integers, Pand w (1 < = P < = 50000, 1 < = w < = 10000). P is the monetary unit value of a coin, W is its gram weight.

Output:

    Print one line of output for each test case. The line must contain "the minimum amount in the deposit tank is X." Where x is the minimum amount that can be achieved using coins of a given total weight. If the weight doesn't arrive exactly, it's impossible to print a line. ".

Samples Input:

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

Samples Output:

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

Code :

#include <iostream>
#include <stdio.h>
#include <limits.h>//or #define INT_MAX 0x7fffffff
#include <vector>
using namespace std;
int T;//T cases
int E,F;//E is empty-coin-bank's weight ,filled-coin-bank's weight
int N;//the number of various coins
int P,W;//P is conin's value, W is conin's weight
struct coin{
	int p;
	int w;
};
vector<int> dp(10001,INT_MAX);
vector<coin> coinsClass(501,{0,0});
int main(){
	cin>>T;
	while(T--){
		cin>>E>>F;
		int V=F-E;//V is coin-bank's capacity
		cin>>N;
		for(int i=1;i<=N;i++){
			cin>>P>>W;
			coinsClass[i]={P,W};
		}
		for(int j=1;j<=V;j++) dp[j]=INT_MAX;
		dp[0]=0;//注:dp[0]=0,dp[1...V]=INT_MAX;
		for(int i=1;i<=n;i++){
			for(int j=coins[i].w;j<=V;j++){//完全,顺序
				if(dp[j-coinsClass[i].w]!=INT_MAX)//限制该物品非整数倍体积,所以最终状态为INT_MAX则容器未装满
					dp[j]=min(dp[j-coinsClass[i].w]+coinsClass[i].p,dp[j]);
			}
//			for(int j=1;j<=V;j++)
//				cout<<dp[j]<<" ";
//			cout<<endl;//检查dp状态矩阵
		}
		if(dp[V]!=INT_MAX)
			cout<<"The minimum amount of money in the piggy-bank is "<<dp[V]<<"."<<endl;
		else
			cout<<"This is impossible."<<endl;
	}
	return 0;
}
/**
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值