Problem Description
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it’s weight in grams.
Output
Print exactly one line of output for each test case. The line must contain the sentence “The minimum amount of money in the piggy-bank is X.” where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line “This is impossible.”.
Sample Input
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
Sample 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.
大概意思:在ACM能够做任何事情之前,必须编制一份预算并获得必要的财政支助。这一行动的主要收入来自不可逆转的约束货币(IBM)。背后的想法很简单。只要有一个ACM成员有零钱,他就会把所有的硬币都扔到存钱罐里。你知道这个过程是不可逆的,不打破猪就不能取出硬币。在足够长的时间之后,存钱罐里应该有足够的现金来支付所有需要支付的东西。
但是存钱罐有一个很大的问题。不可能确定里面有多少钱。因此,我们可能会把猪摔成碎片,结果却发现没有足够的钱。显然,我们希望避免这种令人不快的情况。唯一的可能就是称一称存钱罐,然后试着猜里面有多少硬币。假设我们能够精确地确定猪的重量,并且我们知道某一种货币的所有硬币的重量。然后在存钱罐里有一些我们可以保证的最低数额的钱。你的任务是找出最坏的情况,确定存钱罐里的最低金额。我们需要你帮助。不能再有过早破碎的猪了!
输入。
输入由T测试用例组成。它们的数目(T)是在输入文件的第一行上给出的。每个测试用例都从包含两个整数E和F的行开始,它们指示一只空猪的重量和装满硬币的猪的重量。两种重量都以克为单位。猪的体重不能超过10公斤,这意味着1<;=E<;=F<;=10000。在每个测试用例的第二行,有一个整数N(1<;=N<;=500),它给出了在给定货币中使用的各种硬币的数量。下面是N行,每行指定一种硬币类型。这些线包含两个整数,P和W(1<;=P<;=50000,1<;=W<;=10000)。P是以货币单位表示的硬币的价值,W是以克为单位的重量。
输出量。
只为每个测试用例打印一行输出。这一行必须包含一句话“存钱罐中的最低金额是X”。其中X是使用给定总重量的硬币所能达到的最小金额。如果不能精确地达到重量,请打印一行“这是不可能的”。
样本输入。
三。
10 110。
二。
1 1。
30 50。
10 110。
二。
1 1。
50 30。
1 6。
二。
10 3。
20 4。
样本输出。
存钱罐里的最低金额是60英镑。
存钱罐里的最低限额是100。
这是不可能的。
我的代码:
#include<bits/stdc++.h>
using namespace std;
const int inf =99999999;
long long int dp[50005], a[50005], b[50005];
int main()
{
int t;
cin >> t;
while (t--)
{
long long int e, f,k,n;
cin >> e >> f;
k = f - e;
cin >> n;
for (int i = 1; i <= k; i++)dp[i] = inf;
dp[0] = 0;
for (int i = 1; i <= n; i++)cin >> a[i] >> b[i];
for (int i = 1; i <= n; i++) //以下三列则是精髓
for (int j = b[i]; j <=k; j++)
dp[j] = min(dp[j], dp[j - b[i]] + a[i]);
if (dp[k] == inf)cout << "This is impossible." << endl;
else cout << "The minimum amount of money in the piggy-bank is " << dp[k] << "." << endl;
}
}