题目链接
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.
题目大意:有一个重E的存钱罐,装满后重F(无体积限制);有N种硬币,给出每种硬币的P(价值)和W(重量),求存钱罐能不能装满,能的话求出装满时硬币价值最小是多少,不能输出:This is impossible.

完全背包问题,因为要求装满,所以初始状态为d【0】=0,d【1·······】=无穷大。
状态转移方程:d[j]=min(d[j],d[j-w[i]]+c[i]),w代表硬币重量,c代表硬币价值。

#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int d[10010];
int c[510],w[510];
int main ()
{int t;cin>>t;while(t--){int m,m1,n;memset(d,inf,sizeof(d));d[0]=0;cin>>m1>>m>>n;m-=m1;for(int i=1;i<=n;i++)cin>>c[i]>>w[i];       for(int i=1;i<=n;i++)for(int j=w[i];j<=m;j++)d[j]=min(d[j],d[j-w[i]]+c[i]);if(d[m]==inf)cout<<"This is impossible."<<endl;elsecout<<"The minimum amount of money in the piggy-bank is "<<d[m]<<'.'<<endl;           }
}

小猪存钱罐 完全背包相关推荐

  1. 蓝桥杯刷题013——小猪存钱罐(并查集)

    [题目描述] Byteazar the Dragon 拥有 N 个小猪存钱罐.每一个存钱罐能够用相应的钥匙打开或者被砸开.Byteazar 已经将钥匙放入到一些存钱罐中.现在已知每个钥匙所在的存钱罐, ...

  2. [Poi2005]Piggy Banks小猪存钱罐

    题目描述 Byteazar有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar已经把每个存钱罐的钥匙放到了某些存钱罐里. Byteazar 现在想买一台汽车于是要把所有的钱都取 ...

  3. 合根植物,小猪存钱罐(python)

    文章目录 合根植物 分析 运行代码 通过截图 小猪存钱罐(Piggy Banks) 分析 运行代码 超时截图(超时了) 合根植物 w星球的一个种植园,被分成 m * n 个小格子(东西方向m行,南北方 ...

  4. 洛谷·[POI2005]SKA-Piggy Banks 小猪存钱罐【Tarjan 并查集

    初见安~这里是传送门:洛谷P3420 题目描述3 Byteazar the Dragon has NN piggy banks. Each piggy bank can either be opene ...

  5. [Poi0504]Piggy Banks小猪存钱罐 (并查集)

    Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放到了某些存钱罐里. Byteazar 现在想买一台汽车于是要把所有的钱都取出来. ...

  6. Python:每日一题之小猪存钱罐(并查集)

    题目描述 Byteazar the Dragon 拥有 N 个小猪存钱罐.每一个存钱罐能够用相应的钥匙打开或者被砸开.Byteazar 已经将钥匙放入到一些存钱罐中.现在已知每个钥匙所在的存钱罐,By ...

  7. HDU1114 存钱罐 完全背包

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1114 给你一个存钱罐的空的质量和存满钱的质量,给你每种硬币的质量和价值,让你算出它的最小价值. 3 10 ...

  8. PIPIOJ 1079: PIPI的存钱罐 完全背包

    题目: http://39.106.164.46/problem.php?id=1079 思路: 题目要求装满,然后又是完全背包.注意初始化时将dp初始化为INF,dp[0]=0即可. 代码如下: # ...

  9. HDU and 蓝桥杯 完全背包练习题

    题目链接请点击 我说一下题目的大意吧,第一行给出小猪存钱罐的重量和装满硬币的小猪钱罐的重量,那么能够装硬币的质量是(F-E)g ,(F-E)g中使装的硬币最少,把(F-E)当成容量,用完全背包解决求最 ...

最新文章

  1. popStar手机游戏机机对战程序
  2. 设计模式在工作中的实践
  3. 人类一败涂地电脑版_人类一败涂地游戏正式上架同步推免费版
  4. 上一局APP玩,你画我猜,作为灵魂画手从没服过谁
  5. 最短路径问题的算法实现【转载】
  6. Python中的网络编程之TCP
  7. Android插件化开发基础之App如何动态加载类
  8. 现代化历险记:策略+将COBOL转换为Java的示例
  9. trackingmore快递查询平台_快递物流服务再升级!寄快递更便捷,看看都有哪些平台...
  10. Ranger架构剖析
  11. android鸿洋布局,Android基础ConstrainLayout约束布局的介绍和使用
  12. Flutter高级第3篇:底部 Tab 切换保持页面状态的几种方法
  13. Interesting Housing Problem HDU - 2426 (KM)
  14. 网件 无线打印机服务器,如何设置打印机实现网件Air print功能
  15. 随机抽取学生姓名html,VB中随机抽取学生姓名的程序
  16. 直观理解偏导数、方向导数和法向量和梯度
  17. 嵌入式系统的开发概述(三星s5p6818系统为例)
  18. 模仿百思不得姐项目开发总结
  19. 华为发展鸿蒙再出奇招,学习宝马推出官方认证二手手机
  20. IT运维管理之数据维护技术方案

热门文章

  1. labview调用solidworks3维模型
  2. 现代C++改变了什么
  3. Mathematica中数据类型的互换——实数(即小数)to有理数(即分数),有理数(即分数)to实数(即小数)
  4. (SVN笔记)SVN服务端+SVN客户端Tortoise——安装配置
  5. XT.COM 直播间第106期 | VGO XT.COM AMA 专场
  6. 手机室内地磁定位软件_一种基于地磁的智能手机实时定位方法与流程
  7. math.h 三角函数
  8. 各厂内推整理 | 第三期
  9. cam350菜单怎么切换成中文_CAM350改变有关设置
  10. Camera2使用方法例子代码