Piggy-Bank

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. 题意:已知空小猪罐质量与装满钱时的质量,给出每种钱的价值与质量(每种可有无限个),求出小猪罐中钱币可能存在的最小价值(至少多少钱)。
思路:完全背包。注意最优解为最小值,f初始化INF,f[0]=0。能够保证最后结果f[V]为恰好装满。 ps总结:最大价值memset(f,0,sizeof(f));f[j]=max(f[j],f[j-v[i]]+w[i]); 最小价值memset(f,0,sizeof(f));if(f[j]==0) f[j]=f[j-v[i]]+w[i];else f[j]=min(f[j],f[j-v[i]]+w[i]); 最大价值恰好装满memset(f,-INF,sizeof(f));f[0]=0;f[j]=max(f[j],f[j-v[i]]+w[i]); 最小价值恰好装满memset(f,INF,sizeof(f));f[0]=0;f[j]=min(f[j],f[j-v[i]]+w[i]);
 
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f int f[],v[],w[]; int min(int x,int y)
{
return x<y?x:y;
} int main()
{
int t,V,VV,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d %d",&VV,&V,&n);
V-=VV;
for(i=;i<=n;i++){
scanf("%d%d",&w[i],&v[i]);
}
memset(f,INF,sizeof(f));
f[]=;
for(i=;i<=n;i++){
for(j=v[i];j<=V;j++){
f[j]=min(f[j],f[j-v[i]]+w[i]);
}
}
if(f[V]==INF) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n",f[V]);
}
return ;
}

HDU - 1114 Piggy-Bank 完全背包(背包恰好装满)的更多相关文章

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

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

  2. 题解报告:NYOJ #311完全背包(恰好装满)

    描述: 直接说题意,完全背包定义有N种物品和一个容量为V的背包,每种物品都有无限件可用.第i种物品的体积是c,价值是w.求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大.本题 ...

  3. ACM_01背包(恰好装满)

    背包2 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量刚好为 W ...

  4. HDU 1114:Piggy-Bank(完全背包)

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

  5. QDUOJ 分辣条-01背包恰好装满情况

    分辣条 发布时间: 2016年6月26日 20:36   最后更新: 2016年6月26日 20:37   时间限制: 1000ms   内存限制: 128M 描述 “你喝的酸奶是我买的,辣条也是我买 ...

  6. HDU-1114 完全背包+恰好装满问题

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

  7. HDU 1114 Piggy-Bank (dp)

    题目链接 Problem Description Before ACM can do anything, a budget must be prepared and the necessary fin ...

  8. 题解报告:hdu 1114 Piggy-Bank(完全背包恰好装满)

    Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...

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

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

随机推荐

  1. Linux 日志命令

    当日志文件存储日志很大时,我们就不能用vi直接进去查看日志,需要Linux的命令去完成我们的查看任务 Log位置: /var/log/message 系统启动后的信息和错误日志,是Red Hat Li ...

  2. 一个小公司的前端笔试HTML CSS JS

    网上有这套题的答案,版本也很多,我做了很多参考.本文就当个小笔记,可能有错误,还望指正~ 第1章  Html篇 1. 你做的网页在哪些浏览器测试过?这些浏览器的内核分别是什么? 浏览器类型 内核 Fi ...

  3. BCH硬分叉,BitcoinABC强势逆袭BitcoinSV

        最近币圈讨论最多的就是BCH的硬分叉,今天凌晨BCH硬分叉在众多币圈大佬关注下落下帷幕,此次分叉大站BitcoinABC强势完胜BitcoinSV.吴忌寒在推特上表示:祝贺!在这个新的区块之后 ...

  4. Pandas一些小技巧

    Pandas有一些不频繁使用容易忘记的小技巧 1.将不同Dataframe写在一个Excel的不同Sheet,或添加到已有Excel的不同Sheet(同名Sheet会覆盖) from pandas i ...

  5. Java for LeetCode 102 Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  6. 编写你的第一个django应用程序3

    这一篇从教程第2部分结尾的地方继续讲起.我们将继续编写投票应用,并且专注于如何创建公用界面--也被称为视图 概况 django视图概念是一类具有相同功能和末班的网页的集合,比如,在一个博客应用中,你可 ...

  7. STL容器元素应满足的条件

    要使用C++中的标准模板库中的容器,其元素要满足以下三个条件: 元素必须可以通过copy构造函数进行复制,且二者进行相等测试返回true. 元素必须可以通过赋值操作符完成赋值操作. 元素必须可以通过析 ...

  8. Matlab图像处理(01)-Matlab基础

    枫竹梦对于Matlab几乎是零基础,只是在上学的时候稍稍接触一点,万万没有想到现在还能用到Matlab.进入正题>>> 图像的基本概念 一幅图像可以被定义为一个二维函数f(x,y), ...

  9. HTML5响应式模版Mocha

    HTML5响应式模版Mocha,经典,html5,蓝色,扁平,HTML5响应式模版Mocha是一款宽屏大气的HTML5网站展示模板. http://www.huiyi8.com/moban/

  10. 存储过程之rowtype 使用

    CREATE OR REPLACE PROCEDURE "DYLYLQX_SC_BA_1" (YWID IN VARCHAR2, FLAG OUT VARCHAR2) IS V_R ...