F - Piggy-Bank 完全背包问题
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!
InputThe 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.
OutputPrint 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<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
using namespace std;
#define MAXN 508
#define INF 0x3f3f3f3f
/*
F - Piggy-Bank 典型的完全背包问题,给定容量和不同硬币(价值,重量),求最小价值
*/
int value[MAXN],cost[MAXN],dp[];
int main()
{
int t,e,f,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&e,&f);
memset(dp,INF,sizeof(dp));
int w = f-e;
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%d%d",&value[i],&cost[i]);
}
dp[] = ;
for(int i=;i<m;i++)
for(int v=cost[i];v<=w;v++)
dp[v] = min(dp[v],dp[v-cost[i]]+value[i]);
if(dp[w]==INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[w]);
}
return ;
}
F - Piggy-Bank 完全背包问题的更多相关文章
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 F - Piggy-Bank 【完全背包问题】
https://vjudge.net/contest/68966#problem/F http://blog.csdn.net/libin56842/article/details/9048173 # ...
- ACM Piggy Bank
Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...
- 购物单 && 动态规划 && 背包问题
题目叙述的言语倒是蛮多的: 王强今天很开心,公司发给N元的年终奖.王强决定把年终奖用于购物,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子: 主件 附件 电脑 ...
- silicon labs 代理商
http://www.silabs.com/buysample/pages/contact-sales.aspx?SearchLocation=China Silicon Labs A ...
- Elasticsearch实践(二):搜索
本文以 Elasticsearch 6.2.4为例. 经过前面的基础入门,我们对ES的基本操作也会了.现在来学习ES最强大的部分:全文检索. 准备工作 批量导入数据 先需要准备点数据,然后导入: wg ...
- Android开发训练之第五章第五节——Resolving Cloud Save Conflicts
Resolving Cloud Save Conflicts IN THIS DOCUMENT Get Notified of Conflicts Handle the Simple Cases De ...
- 直接抱过来dd大牛的《背包九讲》来做笔记
P01: 01背包问题 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本思路 这是最 ...
- 2016summer 训练第一场
A.http://acm.hdu.edu.cn/showproblem.php?pid=5538 求表面积,只需要将所有的1*1的小块扫描一遍.将每一个块与他相邻四周进行比较,如果该快高度大,则将该快 ...
- luogu P3420 [POI2005]SKA-Piggy Banks
题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can either be opened with its correspon ...
- 洛谷 P3420 [POI2005]SKA-Piggy Banks
P3420 [POI2005]SKA-Piggy Banks 题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can eith ...
随机推荐
- loadrunner中get和post请求
loadrunner中可以使用web_url和web_link发送get请求,使web_submit_form和web_sumbit_data发送post请求. 有什么不同呢?推荐使用哪一个? web ...
- Django之序列化
关于Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户,特别的Ajax请求一般返回的为Json格式. 1.serializers from django.core ...
- spring简介及常用术语
1.引入 在开发应用时常会遇到如下问题: 1)代码耦合性高: 2)对象之间依赖关系处理繁琐: 3)事务控制繁琐: 2.Spring简介 1)Spring概述 什么是Spring: ①Spring是一个 ...
- uiautomatorviewer详解
一,uiautomatorviewer是什么? Android 4.1发布的,uiautomator是用来做UI测试的.也就是普通的手工测试,点击每个控件元素 看看输出的结果是否符合预期.比如 登陆界 ...
- express模块安装使用命令配置
之前的博客nodejs安装和配置好路径之后就可以安装express了: 随便打开个文件夹右键选择,git bash here 命令行里输入[npm install express -g] -g是全局安 ...
- Table标题行冻结,数据行滚动的一种方式
这段时间在做Table标题行冻结,数据行滚动,虽然能实现,但也遇到一些问题,记录下来. 首先说说实现,实现其实不难,估计很多人都能想象出来,那就是标题行与内容行分离.我是这么做的,用两个表格,一个只有 ...
- 逻辑回归(Logistic Regression)推导
出自BYRans博客:http://www.cnblogs.com/BYRans/ 本文主要讲解分类问题中的逻辑回归.逻辑回归是一个二分类问题. 二分类问题 二分类问题是指预测的y值只有两个取值(0或 ...
- Vue实战之插件 sweetalert 的使用
安装npm install sweetalert2@7.15.1 --save 封装 sweetalertimport swal from 'sweetalert2' export default { ...
- MFC_2.3 定时器、滑块、进度条控件
定时器.滑块.进度条控件 1.拖控件 2.绑定变量.默认,然后取名字 3.初始化设置定时器 // 设置滑块和进度条的范围 m_TrackBar.SetRange(0, 1000); m_StaticP ...
- R语言学习 - 线图一步法
首先把测试数据存储到文件中方便调用.数据矩阵存储在line_data.xls和line_data_melt.xls文件中 (直接拷贝到文件中也可以,这里这么操作只是为了随文章提供个测试文件,方便使用. ...