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.

思路:

1. 完全背包, 且要求恰好装满, dp[0] = 0

2. dp[i][j] 表示前 i 件物品放入容量为 j 的背包的最小价值, dp[i][j] = min(dp[i-1][j-k*w[i]]+k*v[i]])

3. 求 min, 要求初始化为 INF

代码:

#include <iostream>
using namespace std; const int INF = 0X3F3F3F3F;
int E, F, N;
int v[510], w[510];
int dp[10010]; int solve_dp() {
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
int V = F-E;
for(int i = 0; i < N; i++) {
for(int j = w[i]; j <= V; j++) {
dp[j] = min(dp[j], dp[j-w[i]]+v[i]);
}
}
return dp[V];
} int main() {
freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin);
int tc;
cin >> tc;
while(tc--) {
scanf("%d%d%d", &E, &F, &N);
for(int i = 0; i < N; i++)
scanf("%d%d", &v[i], &w[i]);
// mainfunc
int ans = solve_dp();
if(ans == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n", ans);
}
return 0;
}

  

POJ 1384 Piggy-Bank(完全背包)的更多相关文章

  1. poj 1384 Piggy-Bank(全然背包)

    http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...

  2. POJ 1384 Piggy-Bank (完全背包)

    Piggy-Bank 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/F Description Before ACM can d ...

  3. poj 1384 Piggy-Bank(完全背包)

    Piggy-Bank Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10830   Accepted: 5275 Descr ...

  4. POJ 1384 Piggy-Bank【完全背包】+【恰好完全装满】(可达性DP)

    题目链接:https://vjudge.net/contest/217847#problem/A 题目大意:   现在有n种硬币,每种硬币有特定的重量cost[i] 克和它对应的价值val[i]. 每 ...

  5. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  6. POJ 3181 Dollar Dayz(全然背包+简单高精度加法)

    POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...

  7. POJ 3211 Washing Clothes(01背包)

    POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...

  8. POJ 1384 POJ 1384 Piggy-Bank(全然背包)

    链接:http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...

  9. POJ 1384 Piggy-Bank (ZOJ 2014 Piggy-Bank) 完全背包

    POJ :http://poj.org/problem?id=1384 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode ...

  10. POJ 1384 Piggy-Bank 背包DP

    所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...

随机推荐

  1. 用C++画光(二)——矩形

    在上篇文章的基础上,做了许多调整,修复了许多BUG.在解决bug的过程中,我逐渐领悟到一个要领:枯燥地一步步调试太痛苦了,找不到问题的根源!所以我选择将中间结果打到图片上.如: (注意,里面的点是我随 ...

  2. 关于WEB-INF文件夹中的内容

    刚才使用了fileupload的jar包,老样子,我只是在eclipse中将其添加到了project的java build path/libraries中 我以为这样就行了,是的,是行了,仅仅能保证在 ...

  3. 【Visual Studio】设置Solution Explorer同步当前文档

    从Solution Explorer打开查看多个文档,在编辑文档时经常会搞不清楚当前文档在项目中的位置,希望Solution Explorer能够同步跳转到选中当前的文档的位置. 从Debug或Too ...

  4. SpringMVC 利用AbstractRoutingDataSource实现动态数据源切换

    SpringMVC 利用AbstractRoutingDataSource实现动态数据源切换 本文转载至:http://exceptioneye.iteye.com/blog/1698064 Spri ...

  5. windows server 2008 域控安装

    windows server 2008 域控安装:1.dns安装,安装过程会提示.net frame 3.51安装 3.域控安装 原文:地址 http://wenku.baidu.com/link?u ...

  6. Jenkins+ Xcode+ 蒲公英 实现IOS自动化打包和分发

    Jenkins+ Xcode+ 蒲公英 实现IOS自动化打包和分发 直接入正题: Screen Shot 2015-09-18 at 16.56.20.png Mac上安装Jekins jekins下 ...

  7. catalina.home和catalina.base这两个属性的作用

    catalina.home和catalina.base这两个属性仅在你需要安装多个Tomcat实例而不想安装多个软件备份的时候使用,这样能节省磁盘空间.以Tomcat6.0为例,其Tomcat目录结构 ...

  8. vim打开后乱码,xshell打开后也是乱码,设置自动出现行号

    Xshell对于嵌入式开发来说,是个非常不错的工具.但或许都有过被中文显示为乱码的问题感觉有点不爽.解决方法其实很简单的,即把xshell编码方式改成UTF-8即可. [文件]–>[打开]–&g ...

  9. openfire安装配置完全教程

    Java领域的IM解决方案 Java领域的即时通信的解决方案可以考虑openfire+spark+smack. Openfire是基于Jabber协议(XMPP)实现的即时通信服务器端,最新版本是3. ...

  10. MFC 窗体注册 WNDCLASS

    //初始化 //初始化包括窗口类的定义.注册.创建窗口实例和显示窗口四部分 { HWND hwnd; MSG Msg; WNDCLASS wndclass; char lpszClassName[]= ...