HDU - 1114 Piggy-Bank 【完全背包】
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1114
题意
给出一个储钱罐 不知道里面有多少钱
但是可以通过重量来判断
先给出空储钱罐的重量 再给出装满钱币后储钱罐的重量
之后给出N种货币
pi wi
pi 表示 第i件货币的价值
wi 表示第i件货币的重量
最后要求 储钱罐里面最少有多少钱
思路
完全背包模板问题
要注意初始化 因为这个是重量要刚好达到
初始化为 INF
dp[0] = 0
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
typedef pair<int, ll> pil;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
int dp[maxn];
int v[505], w[505];
void init()
{
CLR(dp, 0x3f);
dp[0] = 0;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int a, b;
scanf("%d%d", &a, &b);
int weight = b - a;
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", &v[i], &w[i]);
init();
for (int i = 1; i <= n; i++)
for (int j = w[i]; j <= weight; j++)
dp[j] = min(dp[j], dp[j - w[i]] + v[i]);
if (dp[weight] == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[weight]);
}
}
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(完全背包恰好装满)
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& ...
随机推荐
- (CF#257)A. Jzzhu and Children
There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number al ...
- Java Learning Path(三)过程篇
Java Learning Path(三)过程篇 每个人的学习方法是不同的,一个人的方法不见得适合另一个人,我只能是谈自己的学习方法.因为我学习Java是完全自学的,从来没有问过别人,所以学习的过程基 ...
- UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)
iOS5中,UIViewController新添加了几个方法: - (void)addChildViewController:(UIViewController *)childController N ...
- HTML5 的四个亮点
1.XDM cross-document-messaging 跨文档消息传递. 2.原生拖放功能. 3.新媒体元素 audio.video. 4.历史状态管理.
- SVN 钩子操作-同步更新web目录
一个简单的钩子演示:也可以网上搜索其他高级的 本次想要达到的功能是:每次用户commit 到仓库后,仓库的钩子会自动把程序又更新的www/的web发布目录 1.现在web目录下创建一个test.com ...
- webuploader插件使用中的一点东西
本人绝对菜鸟,高手勿喷 菜鸟开发中的解决方法,高手勿喷 1.针对同一应用中不同的类别,存放不同的路径 在页面中添加,hidden属性的标记,如: type="hidden" ...
- Linux系统控制文件 /etc/sysctl.conf详解
/etc/sysctl.conf这个目录主要是配置一些系统信息,/etc/sysctl.conf参数解释: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...
- 查看tcp各个连接状态的数量
4. 查看tcp各个连接状态的数量 下面对的 netstat -tan|awk '$1~/tcp/{aa[$NF]++}END{for (h in aa)print h,aa[h]}' SYN_SEN ...
- linux 上安装apache 出现 configure: error: APR not found. Please read the documentation错误
今日编译apache时出错: #./configure --prefix……检查编辑环境时出现: checking for APR... noconfigure: error: APR not fou ...
- horizontalDragMaxWidth:0;就没有水平滚动条了
jquery.jscrollpane.css JScrollPane工作所必须的基本的CSS样式.jquery.min.jsjQuery作为javascript库必须提前引入.jquery.mouse ...