Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23646 Accepted Submission(s): 11973
Problem Description
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
Output
Sample Input
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 100.
This is impossible.
Source
//2017-04-04
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int inf = 0x3f3f3f3f;
int dp[], p[], w[];//dp[i]表示重量为i时最少的钱
//状态转移方程:dp[i] = min{dp[i-w[j]]+p[j] | 0<j<n} int main()
{
int T, n, E, F, W;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &E, &F);
W = F-E;
memset(dp, inf, sizeof(dp));
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d%d", &p[i], &w[i]);
if(p[i] < dp[w[i]])//考虑两种硬币重量相等时,选价值小的
dp[w[i]] = p[i];
}
for(int i = ; i <= W; i++){
for(int j = ; j < n; j++){
if(w[j] > i)continue;
if(dp[i-w[j]]+p[j] < dp[i])
dp[i] = dp[i-w[j]]+p[j];
}
}
if(dp[W] == inf)printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[W]);
} return ;
}