HDU 1114 Piggy-Bank 全然背包[通俗易懂]

HDU 1114 Piggy-Bank 全然背包

大家好,又见面了,我是全栈君。

Piggy-Bank

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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.

 

全然背包的基础题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
const int INF = 1e7;
using namespace std;
int dp[1000005],v[600],p[600];
int max(int x,int y)
{
    if(x > y)
        return x;
        else
    return y;
}
int min(int x,int y)
{
    if(x > y)
        return y;
        else
    return x;
}
int main()
{
   int T,VM,Vz,n;
   scanf("%d",&T);
   while(T--)
   {
       scanf("%d%d",&Vz,&VM);
       scanf("%d",&n);
       int st = VM - Vz;
       for(int i = 0;i<=st;i++)
        dp[i] = INF;
        dp[0] = 0;
       for(int i = 1;i<=n;i++) scanf("%d%d",&p[i],&v[i]);
       for(int i = 1;i<=n;i++)
       {
           for(int j = v[i];j<=st;j++)
           {
               dp[j] = min(dp[j-v[i]]+p[i],dp[j]);
           }
       }
       if(dp[st]>=INF)
        puts("This is impossible.");
        else
       printf("The minimum amount of money in the piggy-bank is %d.\n",dp[st]);

    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116618.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • linux awk命令详解

    linux awk命令详解awk是行处理器:相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息awk处理过程:依次对每一行进行处理,然后输出awk命令形式:awk[-F|-f|-v]‘BEGIN{}//{command1;command2}END{}’file[-F|-f|-v]大参数,-F指定分隔符,-f调用脚本,-v定义变量var=…

    2022年7月11日
  • 启动mysql报错10038_解决navicat远程连接mysql报错10038的问题

    navicat远程连接mysql报错10038一般由以下两个原因:一:本地防火墙问题在本地安装了mysql、navicat并打开了mysql服务的情况下,来设置防火墙。首先右击或者点击入站规则,找到新建规则,点击。点击端口。在特定本地端口中填入3306.一直点击下一步。这里可以给一个好分别的名称即可。之前再尝试连接即可,若仍然不可以,可能是服务器方面的问题。二:服务器3306端口未打开首先需要在安…

    2022年4月11日
  • 第三方接口开发流程「建议收藏」

    第三方接口开发流程「建议收藏」1.确定需要哪些接口重点是要确定每个接口的具体功能。确保这些接口是必须的,功能相互间没有交叉。2.接口设计及细节分析a)发送参数名、参数含义、参数数据类型、长度、精度b)接收参数名、参数含义、参数数据类型、长度、精度接口的使用的类型变量尽量通用,特别是对使用此接口的用户一无所知情况下,对方可能是JAVA,也可能是VB6,也可能是C#,不要使用某种编程语言的…

    2022年5月16日
  • android studio 更换darcula主题中的字体颜色以及常用主题「建议收藏」

    android studio 更换darcula主题中的字体颜色以及常用主题「建议收藏」更换主题的网站:http://www.ideacolorthemes.org/home/androidstudio中更换默认主题为darcula的教程网上很多,但是默认darcula主题中的colors&fonts不太喜欢,也感觉比较伤眼睛。国内百度了半天也没有解决方案,无奈翻墙找谷歌发现了这个网站。是在stackoverflow里发现的。设置方法很简单:

    2022年6月27日
  • 怎样让浏览器变身代码编辑器?

    怎样让浏览器变身代码编辑器?将浏览器变成一个简易文本编辑器一开始的功能非常简单,根本没有语法高亮,也没有自动缩进,仅仅是将浏览器变成一个文本编辑器而已。Jose分享的代码如下:data:text/html,htmlcontenteditable>只需要将上面的代码复制粘贴到浏览器的地址栏,然后按回车,就可以让浏览器变成编辑器。是不是非常简单?背后的原理并不高

    2022年6月22日
  • CentOS下netstat命令详解

    CentOS下netstat命令详解为什么80%的码农都做不了架构师?>>>…

    2022年5月30日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号