HDU 1114 Piggy-Bank (完全背包)
Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16255 Accepted Submission(s): 8192
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 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.
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.".
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
The minimum amount of money in the piggy-bank is 100.
This is impossible.
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 10005
#define N 10005
#define INF 0x3f3f3f3f
int f[N];
int w[N];
int d[N];
int n, V, E, F;
int main()
{
int T;
cin >> T; while (T--)
{
scanf("%d%d", &E, &F);
V = F - E;
scanf("%d", &n); for (int i = ; i <= n; i++)
{
scanf("%d%d", &d[i], &w[i]);
} memset(f, INF, sizeof(f));
f[] = ; for (int i = ; i <= n; i++)
for (int v = w[i]; v <= V; v++)
{
if(f[v-w[i]]!=INF && f[v-w[i]]+d[i]<f[v])
f[v] = f[v - w[i]] + d[i];
} if (f[V] != INF)
{
printf("The minimum amount of money in the piggy-bank is %d.\n", f[V]);
}
else
{
printf("This is impossible.\n");
}
}
return ;
}
HDU 1114 Piggy-Bank (完全背包)的更多相关文章
- HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)
HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...
- HDU 1114 Piggy-Bank(完全背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题目大意:根据储钱罐的重量,求出里面钱最少有多少.给定储钱罐的初始重量,装硬币后重量,和每个对应 ...
- HDU - 1114 Piggy-Bank 【完全背包】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意 给出一个储钱罐 不知道里面有多少钱 但是可以通过重量来判断 先给出空储钱罐的重量 再给出装 ...
- 题解报告:hdu 1114 Piggy-Bank(完全背包恰好装满)
Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...
- hdu(1114)——Piggy-Bank(全然背包)
唔..近期在练基础dp 这道题挺简单的(haha).可是我仅仅想说这里得注意一个细节. 首先题意: 有T组例子,然后给出储蓄罐的起始重量E,结束重量F(也就是当它里面存满了零钱的时候).然后给你一个数 ...
- HDU 1114 Piggy-Bank ——(完全背包)
差不多是一个裸的完全背包,只是要求满容量的最小值而已.那么dp值全部初始化为inf,并且初始化一下dp[0]即可.代码如下: #include <stdio.h> #include < ...
- HDU - 1114 Piggy-Bank(完全背包讲解)
题意:背包重量为F-E,有N种硬币,价值为Pi,重量为Wi,硬币个数enough(无穷多个),问若要将背包完全塞满,最少需要多少钱,若塞不满输出“This is impossible.”. 分析:完全 ...
- HDU 1114 完全背包 HDU 2191 多重背包
HDU 1114 Piggy-Bank 完全背包问题. 想想我们01背包是逆序遍历是为了保证什么? 保证每件物品只有两种状态,取或者不取.那么正序遍历呢? 这不就正好满足完全背包的条件了吗 means ...
- Piggy-Bank(HDU 1114)背包的一些基本变形
Piggy-Bank HDU 1114 初始化的细节问题: 因为要求恰好装满!! 所以初始化要注意: 初始化时除了F[0]为0,其它F[1..V]均设为−∞. 又这个题目是求最小价值: 则就是初始化 ...
- HDU 1114 Piggy-Bank(一维背包)
题目地址:HDU 1114 把dp[0]初始化为0,其它的初始化为INF.这样就能保证最后的结果一定是满的,即一定是从0慢慢的加上来的. 代码例如以下: #include <algorithm& ...
随机推荐
- python基础——10(三元运算符、匿名函数)
一.三元运算符 本质是if--else--的语法糖 前提:简化if--else--的结构,且两个分支有且只有一条语句 案例: a = 20 b = 30 res = a if a > b els ...
- 输入url后发生了什么
(1)浏览器解析 (2)查询缓存 (3)DNS查询 顺序如下,若其中一步成功直接进去建立连接部分: -- 浏览器自身DNS -- 操作系统DNS -- 本地hosts文件 -- 像域名服务器发送请求 ...
- acm之简单博弈 Nim Bash Wythoff
前些日子我打算开了博弈基础,事后想进行总结下 一句话就是分析必胜或必败,异或为0. 以下内容来自转载: Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠 ...
- Java日志实战及解析
Java日志实战及解析 日志是程序员必须掌握的基础技能之一,如果您写的软件没有日志,可以说你没有成为一个真正意义上的程序员. 为什么要记日志? • 监控代码 • 变量变化情况, ...
- Android点击按钮拨打电话
代码改变世界 Android点击按钮拨打电话 public void callPhone(String str) { Intent intent=new Intent(); intent.setAct ...
- [luoguP2219] [HAOI2007]修筑绿化带(单调队列)
传送门 需要n*m的算法,考虑单调队列 可以预处理出来 a[i][j]表示以i,j为右下角的绿化带+花坛的和 b[i][j]表示以i,j为右下角的花坛的和 那么我们可以单调队列跑出来在A-C-1,B- ...
- 北京集训TEST16——图片加密(fft+kmp)
题目: Description CJB天天要跟妹子聊天,可是他对微信的加密算法表示担心:“微信这种加密算法,早就过时了,我发明的加密算法早已风靡全球,安全性天下第一!” CJB是这样加密的:设CJB想 ...
- java.lang.Class解析
java.lang.Class 1.java.lang.Class的概念 当一个类或接口被装入的JVM时便会产生一个与之关联的java.lang.Class对象,java.lang.class类就是用 ...
- c++ primer note
---恢复内容开始--- 1.decltype 2.auto 3.cbegin 4.cend 5.constexpr 6.(*Parray)[10]=&arr; //Parray 指向一个含有 ...
- MySQL-JDBC Loadbalance深入解析
背景说明 公司的整个电商系统搭建在华为云上,根据老总的估计,上线3个月之后日订单量会达到百万级别,保守估计3个月之后总订单个数预计会有5千万.MySQL单表达到千万级别,就会出现明显的性能问题.根据如 ...