Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23354    Accepted Submission(s): 11824

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.
 
Source
 题意:
n种物品无数个,每种物品有体积和价值,问恰好装满时的最小价值。
代码:
//多重背包,要最小价值,f初始化为无穷大(求最大价值时f[i]=-oo,f[0]=0),f[0]=0,
//求最小值即可。这里用inf初始化f,结果不对!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x7fffffff;
int f[];
struct coin{
int p,w;
double c;
}coins[];
void ComplitPack(int v,int w,int tol)
{
for(int i=v;i<=tol;i++){
f[i]=min(f[i],f[i-v]+w);
}
}
int main()
{
int t,n,s,e;
scanf("%d",&t);
while(t--){
scanf("%d%d",&s,&e);
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&coins[i].p,&coins[i].w);
coins[i].c=coins[i].p/coins[i].w;
}
for(int i=;i<=e-s;i++) f[i]=;
f[]=;
for(int i=;i<n;i++){
ComplitPack(coins[i].w,coins[i].p,e-s);
}
if(f[e-s]!=)
printf("The minimum amount of money in the piggy-bank is %d.\n",f[e-s]);
else printf("This is impossible.\n");
}
return ;
}

HDU1114 背包的更多相关文章

  1. HDU-1114(背包DP)

    Piggy-Bank Problem Description Before ACM can do anything, a budget must be prepared and the necessa ...

  2. 简单的完全背包HDU1114

    今天广州下雨啦,不过没关系啦,反正我最近也都在刷题学习算法. 昨天做了五题01背包,今天还是背包,不过是完全背包,估计做动态规划要持续好一段时间,一开始选了一道简单题目啦. HDU1114,看了小一段 ...

  3. hdu1114 完全背包

    题意:给出钱罐的重量,然后是每种钱的价值和重量,问钱罐里最少可能有多少钱. 完全背包. 代码: #include<iostream> #include<cstdio> #inc ...

  4. hdu1114 Piggy-Bank (DP基础 完全背包)

    链接:Piggy-Bank 大意:已知一只猪存钱罐空的时候的重量.现在的重量,已知若干种钱的重量和价值,猪里面装着若干钱若干份,求猪中的钱的价值最小值. 题解: DP,完全背包. g[j]表示组成重量 ...

  5. hdu1114(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 分析:很裸的一道完全背包题,只是这里求装满背包后使得价值最少,只需初始化数组dp为inf:dp[ ...

  6. 完全背包hdu1114

    https://vjudge.net/contest/68966#problem/F 初始化就行了:dp[0]=0: 这题还要刚好装满背包,输出时进行判断 #include<map> #i ...

  7. hdu1114 dp(完全背包)

    题意:已知空钱罐质量和满钱罐质量(也就是知道钱罐里的钱的质量),知道若干种钱币每种的质量以及其价值,钱币都是无限个,问最少钱罐中有多少钱. 这个题在集训的时候学长给我们做过,所以你会做是应该的,由于已 ...

  8. kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. HDU1114(完全背包装满问题)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. Intro to Probabilistic Model

    概率论复习 概率(Probability) 频率学派(Frequentist):由大量试验得到的期望频率(致命缺陷:有些事情无法大量试验,例如一封邮件是垃圾邮件的概率,雷达探测的物体是一枚导弹的概率) ...

  2. WCF:REST + Basic authentification + IIS

    近期一个项目中用到Restful WCF提供服务,但是需要验证机制,网上搜刮了一些,都是太复杂.FQ找到了一篇不错的文章分享一下. 原地址连接:http://vgolovchenko.wordpres ...

  3. 感知机(perceptron)

  4. Centos6更新yum repo

    163开源镜像站是国内比较老的一个网站.很多人都在使用. step 1/3 备份原镜像文件: cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.r ...

  5. POJ 2187 Beauty Contest(凸包+旋转卡壳)

    Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...

  6. Ubuntu 配置 Android 开发 环境

    . 果断换Ubuntu了, Ubuntu的截图效果不好, 不能设置阴影 ... 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article ...

  7. Alpha 冲刺(2/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助前端界面的开发 搭建测试用服务器的环境 完成 ...

  8. Java容器之Iterator接口

    Iterator 接口: 1. 所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象. 2. Iterator 对象称作迭代器,用以方便的 ...

  9. LintCode-365.二进制中有多少个1

    二进制中有多少个1 计算在一个 32 位的整数的二进制表式中有多少个 1. 样例 给定 32 (100000),返回 1 给定 5 (101),返回 2 给定 1023 (111111111),返回 ...

  10. JDK源码分析 – ArrayList

    ArrayList类的申明 ArrayList是一个支持泛型的,底层通过数组实现的一个可以存任意类型的数据结构,源码中的定义如下: public class ArrayList<E> ex ...