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 ...
随机推荐
- 去掉vue 中的代码规范检测(Eslint验证)
去掉vue 中的代码规范检测(Eslint验证): 1.在搭建vue脚手架时提示是否启用eslint检测的. Use ESLint to lint your code? 写 no; 2.如果项目已经生 ...
- bzoj1010: [HNOI2008]玩具装箱toy(斜率优化DP)
Orz CYC帮我纠正了个错误.斜率优化并不需要决策单调性,只需要斜率式右边的式子单调就可以了 codevs也有这题,伪·双倍经验233 首先朴素DP方程很容易看出:f[i]=min(f[j]+(i- ...
- 【简单算法】40.Fizz Buzz
题目: 写一个程序,输出从 到 n 数字的字符串表示. . 如果 n 是3的倍数,输出“Fizz”: . 如果 n 是5的倍数,输出“Buzz”: .如果 n 同时是3和5的倍数,输出 “FizzBu ...
- 从MYSQL数据库查出指定格式的日期
1.用SQL语言控制: 格式如下: select DATE_FORMAT(t.startTime,"%Y-%m-%d %H:%i") AS startTime, DATE_FORM ...
- MYSQL性能察看
http://fengbin2005.iteye.com/blog/1580214 网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步 ...
- Hbase万亿级存储性能优化总结
背景 hbase主集群在生产环境已稳定运行有1年半时间,最大的单表region数已达7200多个,每天新增入库量就有百亿条,对hbase的认识经历了懵懂到熟的过程.为了应对业务数据的压力,hbase入 ...
- uboot主Makefile分析(t配置和编译过程详解)
1.编译uboot前需要三次make make distcleanmake x210_sd_configmake -j4 make distclean为清楚dist文件. make x210_sd_c ...
- Mybatis(4) 映射文件-参数处理
参数处理: 单参数处理: mybatis 不会做任何特殊处理. #{key} : key 可以写任何字段取出参数值. 测试方法: mapper接口: mapper.xml: 控制台: 多参数处理: m ...
- Cppcheck代码分析上
1.检查点 1.自动变量检查: 返回自动变量(局部变量)指针: 2.越界检查:数组越界返回自动变量(局部变量)指针: 3.类检查:构造函数初始化: 4.内存泄露检查: 5.空指针检查: 6.废弃函数 ...
- 01-QQ 3-最终重构版
Demo示例程序源代码
源代码下载链接:01-QQ 3.zip292.5 KB // QQAppDelegate.h Map // // QQAppDelegate.h // 01-QQ // // Created ...