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
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.
 
 
分析:题意类似于背包问题,每个钱币都有价值和重量,钱罐总重量告诉你,问你刚好使钱罐装满的最小钱币价值之和是多少?
     一开始想过用贪心,即根据每个钱币的价值与重量之比从小到大来装,后来发现wa,于是考虑用dp。写完测试发现怎么也不对,最后原来是没把dp[0]设为零。。。。
 #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
using namespace std;
#define INF 1000000
typedef long long ll;
const int maxn=;
struct money{
int p,w;
}mm[maxn];
int dp[];
int main()
{
int t;
scanf("%d",&t);
while(t--){
int e,f;
scanf("%d%d",&e,&f);
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&mm[i].p,&mm[i].w);
}
int tot=f-e;
for(int i=;i<=tot;i++) dp[i]=INF;
dp[]=;
for(int i=;i<n;i++){
for(int v=mm[i].w;v<=tot;v++){
dp[v]=min(dp[v],dp[v-mm[i].w]+mm[i].p);
}
}
if(dp[tot]!=INF) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[f-e]);
else printf("This is impossible.\n");
}
return ;
}

HDU-1114(背包DP)的更多相关文章

  1. HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)

    HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...

  2. Piggy-Bank(HDU 1114)背包的一些基本变形

    Piggy-Bank  HDU 1114 初始化的细节问题: 因为要求恰好装满!! 所以初始化要注意: 初始化时除了F[0]为0,其它F[1..V]均设为−∞. 又这个题目是求最小价值: 则就是初始化 ...

  3. HDU - 1059 背包dp

    题目: 有两个小朋友想要平分一大堆糖果,但他们不知道如何平分需要你的帮助,由于没有spj我们只需回答能否平分即可. 糖果大小有6种分别是1.2.3.4.5.6,每种若干颗,现在需要知道能不能将这些糖果 ...

  4. hdu 1171 Big Event in HDU(背包DP)

    题意: 杭电搬迁,有N种设备,每种设备有个价值V,数量M,要求将这些设备平分,使得平分后两边的总价值尽可能地相等. 输出两边各自的总价值. 思路: 背包DP后,P=所有的总价值/2,然后从P开始往两边 ...

  5. HDU 1114 【DP】

    题意: 给你空钱袋的质量和装满钱的钱袋的质量. 给你先行的n种货币的面值和质量. 问钱包里的钱最少是多少. 如果质量不可行,输出impossible. 思路: 完全背包. 屌丝有个地方没想通,就是如何 ...

  6. HDU-1171 Big Event in HDU(生成函数/背包dp)

    题意 给出物品种类,物品单价,每种物品的数量,尽可能把其分成价值相等的两部分. 思路 背包的思路显然是用一半总价值当作背包容量. 生成函数则是构造形如$1+x^{w[i]}+x^{2*w[i]}+.. ...

  7. HDU 1114 Piggy-Bank(一维背包)

    题目地址:HDU 1114 把dp[0]初始化为0,其它的初始化为INF.这样就能保证最后的结果一定是满的,即一定是从0慢慢的加上来的. 代码例如以下: #include <algorithm& ...

  8. HDU 1114(没有变形的完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 Piggy-Bank Time Limit: 2000/1000 MS (Java/Others ...

  9. hdu 1114 dp动规 Piggy-Bank

    Piggy-Bank Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S ...

  10. HDU 1114 完全背包 HDU 2191 多重背包

    HDU 1114 Piggy-Bank 完全背包问题. 想想我们01背包是逆序遍历是为了保证什么? 保证每件物品只有两种状态,取或者不取.那么正序遍历呢? 这不就正好满足完全背包的条件了吗 means ...

随机推荐

  1. Android 图片比较

    Android中图片比较大致的流程如下: 将Drawable对象转化成Bitmap对象.(实际比较的是Bitmap对象,假设为B1和B2) 将B1和B2中的像素取出,分别放在2个数组中.(实际存储的是 ...

  2. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

  3. Java NIO原理及实例

    Java NIO是在jdk1.4开始使用的,它既可以说成“新I/O”,也可以说成非阻塞式I/O.下面是java NIO的工作原理: 1. 由一个专门的线程来处理所有的 IO 事件,并负责分发. 2. ...

  4. Light OJ 1060 - nth Permutation(组合数)

    题目大意: 给你一个字符串,问这个字符串按照特定顺序排列之后,第n个字符串是哪个? 题目分析: 首先我们要会求解总个数.也就是共有len个字符,每个字符有ki个,那么总组合方式是多少种? 总组合方式就 ...

  5. Net中的反射使用入门

    [转载] MSDN:ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpguide/html/cpcondiscoveringtypeinformatio ...

  6. 数学类杂志SCI2013-2014影响因子

    ISSN Abbreviated Journal Title Full Title Category Subcategory Country total Cites IF        2013-20 ...

  7. .NET混淆工具 (Dotfuscator Professional Edition)

      如果不想自己辛辛苦苦码出来的作品被人轻易的破解cpoy就使用这款工具试试吧.^_^   使用版本:4.9.7500.9484 ,破解版:http://pan.baidu.com/s/1dDH7lr ...

  8. Bzoj 3173: [Tjoi2013]最长上升子序列 平衡树,Treap,二分,树的序遍历

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1183  Solved: 610[Submit][St ...

  9. 大众点评的大数据实践-CSDN.NET

    大众点评的大数据实践-CSDN.NET 大众点评的大数据实践 爬虫工程师成大数据时代的"宠儿" - 杭州新闻中心 - 杭州网 爬虫工程师成大数据时代的"宠儿"

  10. Java 字符转码之UTF-8转为GBK/GB2312

    java跟python类似的做法,在java中字符串的编码是java修改过的一种Unicode编码,所以看到java中的字符串,心理要默念这个东西是java修改过的一种Unicode编码的编码. pa ...