题目描述

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!

输入

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.

输出

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.".

完全背包 且此题的背包容量必须等于物品总容量不能有空余! 且是寻找最小总价值!

使用空间优化,背包容量正序遍历,这点与01背包是不同的,空间优化的01背包采用倒叙遍历。

比普通01背包初始化情况不同,INF。

进行dp时,需要额外附加一条判断条件 判断历史状态是否可以获得 ,这是恰好装满背包问题必须要注意的一点

#include<stdio.h>
#include<algorithm>
#define INF 0x7fffffff
using namespace std;
struct coin
{int v;int w;
}list[505];
int dp[10005];
int main()
{int t;scanf("%d",&t);int e,f;int n;while(t--){scanf("%d%d",&e,&f);f-=e;//f是背包容量 scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d",&list[i].v,&list[i].w);}//背包必须恰好装满 初始化与普通背包初始化不同 for(int i=0;i<=f;i++){dp[i]=INF;}dp[0]=0;//尽管是完全背包 但是依然得按照从第个一物品到最后一个物品的顺序考虑添加到包里//外层循环不能少 for(int i=1;i<=n;i++){//题目要求最小钱数 所以上面是正INF 这里用min for(int j=list[i].w;j<=f;j++){//这里这句判断必不能少,少了就错 与正确答案是两种情况//这一句判断是恰好装满背包问题必须要加注意的一句代码 //如果dp[j-list[i].w]!=INF即dp[j-list[i].w]是可到达的,可以由这种情况转移而来 if(dp[j-list[i].w]!=INF)dp[j]=min(dp[j],dp[j-list[i].w]+list[i].v);}}if(dp[f]!=INF){printf("The minimum amount of money in the piggy-bank is %d.\n",dp[f]);}else{printf("This is impossible.\n");}}  return 0;
} 

piggy bank 完全背包相关推荐

  1. java实现动态规划算法解决存钱罐问题(piggy bank)

    一.实验目的 练习使用动态规划算法解决实际问题(使用Java语言实现) 二.实验内容 [问题描述] 给定一个空存钱罐的重量和这个存钱罐最多能装进去的重量,现在需要在不打破这个存钱罐的情况下猜测里面最少 ...

  2. 循环 直到 python_如果您在Python中存在慢循环,则可以对其进行修复……直到无法解决为止...

    循环 直到 python by Maxim Mamaev 马克西姆·马马耶夫(Maxim Mamaev) Let's take a computational problem as an exampl ...

  3. 这段时间做的简单dp题目(部分)

    这些时间vj上做的部分题目 HDU5115 题意:第一行t,t组测试数据,每组数据第一行输入n表示n匹狼,第二行给出一个序列表示每匹狼的伤害,第三行给出每匹狼能给周围狼的伤害增幅,求怎样打可以得到最小 ...

  4. ImageNet1000分类,英文原版,中文翻译版

    在训练模型时,可以用imagenet中的图片进行分类学习,imagenet中分类介绍 一.官网网址 imagenet官网网址 1-398:动物 399-924:物品 925-1000:食物 二.官方英 ...

  5. 独家 | 人工神经网络中发现了人类大脑拥有的多模态神经元(附链接)

    作者:Gabriel Goh, Chelsea Voss, Daniela Amodei, Shan Carter, Michael Petrov, Justin Jay Wang, Nick Cam ...

  6. 一步步学习如何安装并使用SAP HANA Express Edition

    使用Jerry这篇文章在Google Cloud platform上的Kubernetes集群部署HANA Express里介绍的方法在Google Cloud Platform的Kubernetes ...

  7. TensorFlow神经网络(九)VGG net论文阅读笔记

    [注]内容来自MOOC人工智能实践TensorFlow笔记课程第8讲 来源:2015 ICLR 用于图像分类的文章: Very Deep Convolutional Networks for Larg ...

  8. ValueError: You are trying to load a weight file containing 0 layers into a model with 16 layers.

    ValueError: You are trying to load a weight file containing 0 layers into a model with 16 layers. 在使 ...

  9. imagenet数据集类别标签和对应的英文中文对照表

    预测结果输出one-hot类型,最大概率的下标即为对于类别号   0: 'tench, Tinca tinca',                             丁鲷(鱼) 1: 'gold ...

最新文章

  1. python 博弈论 库_SHAP:Python的可解释机器学习库
  2. asp.net使用for循环实现Datalist的分列显示功能
  3. BugKuCTF WEB web3
  4. Uva536 Tree Recovery二叉树重建(先序和中序确定二叉树,后序输出)
  5. Python 第三方库之docx
  6. 灵活强大的构建系统Gradle
  7. Android自定义组合布局,Android 流式布局 + 自定义组合控件
  8. zookeeper之系列五:简单操作
  9. Cowboy 源码分析(二十六)
  10. 学习软件设计模式的书籍
  11. SSL证书是什么?SSL证书一般要多少钱
  12. Excel怎么合并单元格
  13. windows7系统,ping本机ip地址请求超时的解决方案
  14. 使用jquery获取标签的id属性
  15. 原来,我连一个URL都写不对…
  16. 【人工智能毕设之基于Python+flask+bilstm的评论情感分析系统-哔哩哔哩】 https://b23.tv/QU56eTl
  17. html怎么把按钮做成可以百度,网站网页中加入各种分享按钮功能 百度分享
  18. 新浪微博关注html代码,Jquery实现仿新浪微博获取文本框能输入的字数代码
  19. Python爬虫入门教程 35-100 知乎网全站用户爬虫 scrapy
  20. python写所有大写、小写、大小写、字母

热门文章

  1. 跨境电商「独角兽」融资40亿+,这家公司是怎么做增长的?
  2. 机器人瓦力 配乐_机器人瓦力中的插曲是什么?
  3. 【Linux】vim全选,全部复制,全部删除
  4. L1-5 试试手气(c++、数组)
  5. C++ 友元(friend)
  6. html ide iOS,用于ios开发的ide是 ios开发的ide有哪些
  7. 网络游戏通讯模型初探
  8. 炒作房地产对个人三观之扭曲
  9. 面试笔记(51信用卡-Java开发实习)
  10. linux kernel --- checksum相关ip_summed和feature字段解释