Piggy-Bank-HDU1114-完全背包

不念不忘少年蓝@ 2022-06-05 05:56 247阅读 0赞

Piggy-Bank

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

  1. 3
  2. 10 110
  3. 2
  4. 1 1
  5. 30 50
  6. 10 110
  7. 2
  8. 1 1
  9. 50 30
  10. 1 6
  11. 2
  12. 10 3
  13. 20 4

Sample Output

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

题意:一个存钱罐,给空的存钱罐的质量和装足存钱罐的质量,给n种硬币的质量和价值,求存钱罐中最小的价值。完全背包。

点击打开链接

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define INF 0x3f3f3f3f
  4. int v[550];
  5. int w[550];
  6. int dp[10010];
  7. int W;
  8. int main()
  9. {
  10. int t,e,f,n;
  11. scanf("%d",&t);
  12. while(t--)
  13. {
  14. scanf("%d%d",&e,&f);
  15. W=f-e;
  16. scanf("%d",&n);
  17. for(int i=0;i<n;i++)
  18. {
  19. scanf("%d %d",&v[i],&w[i]);
  20. }
  21. for(int i=0;i<=W;i++)
  22. {
  23. dp[i]=INF;
  24. }
  25. dp[0]=0;
  26. for(int i=0;i<n;i++)
  27. {
  28. for(int j=w[i];j<=W;j++)
  29. {
  30. if(dp[j-w[i]]<INF)
  31. dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
  32. }
  33. }
  34. if(dp[W]>=INF)printf("This is impossible.\n");
  35. else
  36. printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]);
  37. }
  38. return 0;
  39. }

发表评论

表情:
评论列表 (有 0 条评论,247人围观)

还没有评论,来说两句吧...

相关阅读

    相关 完全背包

    完全背包在背包九讲中有很详细的讲解,但是今天碰到题尝试了一下他给的算法,发现并不快,看了一下其他的代码,速度很快! 题目链接http://hihocoder.com/prob

    相关 01背包,完全背包

    01背包问题:一个背包总容量为V,现在有N个物品,第i个 物品体积为weight\[i\],价值为value\[i\],现在往背包里面装东西,怎么装能使背包的内物品价值最大?

    相关 完全背包

    完全背包和01背包的区别 > 01背包:有n个物品,每种物品只能被使用一次 > > 完全背包:有n个物品,每个物品可以被多次使用 完全背包的递推公式可以由01背包的递推公

    相关 完全背包

    问题: 有N种物品和一个容量为V的背包,每种物品都有无限件可用。第i种物品的费用是c\[i\],价值是w\[i\]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量