hdu 1114(完全背包)
Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18924 Accepted Submission(s): 9579
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.
分析:每种物品无限多,可以分析出来是个完全背包问题.dp[i]代表钱罐重量为i时能够装的最少价值。
状态转移方程 dp[i] = min(dp[i],dp[i-V[i]]+W[i]) 其中V[i]是正向枚举的,因为每种物品无限多.
///分析:每种物品无限多,可以分析出来是个完全背包问题.dp[i]代表钱罐重量为i时能够装的最少价值。
///状态转移方程 dp[i] = min(dp[i],dp[i-V[i]]+W[i]) 其中V[i]是正向枚举的,因为每种物品无限多.
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include <math.h>
#define N 10005
using namespace std; int dp[N];
int V[],W[];
int INF = ;
int main()
{
int tcase ;
scanf("%d",&tcase);
while(tcase--){
int E,F,n;
scanf("%d%d",&E,&F);
F-=E;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&W[i],&V[i]);
}
for(int i=;i<=F;i++) dp[i] = INF;
dp[] = ; ///初始化重量为0的背包能够装的最大价值为0
for(int i=;i<=n;i++){
for(int v=V[i];v<=F;v++){
dp[v] = min(dp[v],dp[v-V[i]]+W[i]);
}
}
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 ;
}
hdu 1114(完全背包)的更多相关文章
- HDU 1114 完全背包 HDU 2191 多重背包
HDU 1114 Piggy-Bank 完全背包问题. 想想我们01背包是逆序遍历是为了保证什么? 保证每件物品只有两种状态,取或者不取.那么正序遍历呢? 这不就正好满足完全背包的条件了吗 means ...
- HDU 1114 完全背包+判断能否装满
题意 给出一个存钱罐里的钱币重量 给出可能的n种钱币重量以及价值 求存钱罐中钱币的最小价值 若不可能另有输出 在裸的完全背包上加了一点东西 即判断这个背包能否被装满 初始化 dp[0]=0 其余的都使 ...
- Piggy-Bank HDU - 1114 完全背包
#include<iostream> #include<cstring> using namespace std; const int INF=0x3f3f3f3f; ]; s ...
- HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)
HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...
- HDU 1114 Piggy-Bank 完全背包 dp
http://acm.hdu.edu.cn/showproblem.php?pid=1114 完全背包的题目,要求输出最小价值.然后一定要把给出的背包重量全部用完. 就是问一个背包为k的大小,n件物品 ...
- 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& ...
- hdu 1114 dp动规 Piggy-Bank
Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- poj 1114 完全背包 dp
如果可以每个物品拿多件,则从小到大遍历,否则从大到小遍历. G - Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO ...
随机推荐
- input file上传图片预览,非插件
Input标签 <input type="file" name="pic" onchange="changepic(this)" mu ...
- html 5 新特性
现在html 5技术是最新的html标准,掌握html 5已经变得非常重要,以下是我查看相关资料后对html 5 的新特性的总结,方便大家对比学习.html 5的新特性1.取消了一些过时的html 4 ...
- LightOJ 1319 - Monkey Tradition CRT除数互质版
本题亦是非常裸的CRT. CRT的余数方程 那么定义 则 其中 为模mi的逆元. /** @Date : 2016-10-23-15.11 * @Author : Lweleth (SoungEarl ...
- Linux修改用户密码
1. root修改自己 # passwd 2. root修改别人 # passwd oracle //修改oracle的密码
- CentOS 7 vim显示中文乱码
使用xshell的时候,发现有时候中文显示有乱码,一开始以为是Xshell没设置好,后来检查了一下xshell<<文件<<属性<<终端:右侧编码,显示的是Unico ...
- 如何设计一个优雅健壮的Android WebView?(下)
转:如何设计一个优雅健壮的Android WebView?(下) 前言 在上文<如何设计一个优雅健壮的Android WebView?(上)>中,笔者分析了国内WebView的现状,以及在 ...
- GitLab 迁移与升级
参考: [ 博客园 BigBao ] 环境说明: OS: CentOS 7.x gitlab-ce 初始版本: 8.8.5 gitlab-ce 升级到版本: 11.2.3 升级方式: rpm 安装升级 ...
- 4、什么是事务?MySQL如何支持事务?
什么是事务? 事务是由一步或几步数据库操作序列组成逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行.程序和事务是两个不同的概念.一般而言:一段程序中可能包含多个事务.(说白了就是几步的数据库操作 ...
- Python学习笔记 - day9 - 模块与包
模块与包 一个模块就是一个包含了Python定义和声明的文件,文件名就是模块名加上.py的后缀,导入一个py文件,解释器解释该py文件,导入一个包,解释器解释该包下的 __init__.py 文件,所 ...
- Yii 1.1.17 四、属性标签、AR类增删改查、使用上传类与扩展第三方类库
一.属性标签与规则设置 当进入网站页面,将会读数据库返回信息到视图上.那么,现在定义模型中的属性在视图标签上的显示, 也就是模型属性到前台标签的映射 // 定义模型属性到前台标签的映射 public ...