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;
		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

 */

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章