HDU 1114 Piggy-Bank 完全背包 dp
http://acm.hdu.edu.cn/showproblem.php?pid=1114
完全背包的题目,要求输出最小价值。然后一定要把给出的背包重量全部用完。
就是问一个背包为k的大小,n件物品,能装的最小价值,并且一定是用了k个背包容量。
用dp[i]表示背包容量为i得时候,能收录的最小价值,
边界:dp[0] = 0; 没容量,啥都干不了
else dp[i] = inf。一开始初始化为无穷大。
转移的话,dp[i] = min(dp[i], dp[i - weight[j]] + val[j])
枚举的话,次循环要顺着枚举。
因为这样才能确保它是能使用多次(完全背包嘛)
为什么顺着枚举就可以了呢?
因为考虑一下,当物品的重量为5,背包重量是10的时候。
顺着枚举for (int j = 5; j <= 10; ++j) 的话,
j = 5的时候,物品能枚举到是否加入背包,然后j = 10的时候,物品再次被枚举是否加入这个背包,也就是重复使用了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int dp[maxn];
int val[maxn];
int weight[maxn];
void work() {
int a, b;
scanf("%d%d", &a, &b);
int num = b - a;
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d%d", &val[i], &weight[i]);
}
memset(dp, 0x3f, sizeof dp);
dp[] = ; //拥有0元,啥都干不了
for (int i = ; i <= n; ++i) {
for (int j = weight[i]; j <= num; ++j) {
dp[j] = min(dp[j], dp[j - weight[i]] + val[i]);
}
}
if (dp[num] != inf) {
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[num]);
} else {
printf("This is impossible.\n");
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
HDU 1114 Piggy-Bank 完全背包 dp的更多相关文章
- HDU 5501 The Highest Mark 背包dp
The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 1011 Starship Troopers 树形+背包dp
http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:每个节点有两个值bug和brain,当清扫该节点的所有bug时就得到brain值,只有当父节点被 ...
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
- 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 2602 Bone Collector (01背包DP)
题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...
- 题解报告: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 5616 Jam's balance 背包DP
Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...
- HDU 1114 Piggy-Bank ——(完全背包)
差不多是一个裸的完全背包,只是要求满容量的最小值而已.那么dp值全部初始化为inf,并且初始化一下dp[0]即可.代码如下: #include <stdio.h> #include < ...
随机推荐
- 常见排序算法-php
1.归并排序 $a = [1, 4, 6, 8, 10, 14, 16]; $b = [2, 3, 5, 8, 9, 11]; function merge_sort($a, $b) { $a_i = ...
- codeforces 659E E. New Reform(图论)
题目链接: E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 集训Day7
在做过的试题里ran的...发现之前做的题有些已经生疏了 bzoj3626 LCA 一棵树,每次询问在$[l,r]$区间内的每个节点$i$与$z$的最近公共祖先的深度之和 假的LCA 有一个很平凡的想 ...
- Codeforces Gym 101190 NEERC 16 .L List of Primes(递归)
ls特别喜欢素数,他总是喜欢把素数集合的所有子集写下来,并按照一定的顺序和格式.对于每一个子集,集合内 的元素在写下来时是按照升序排序的,对于若干个集合,则以集合元素之和作为第一关键字,集合的字典序作 ...
- codevs 1144 守望者的逃离
传送门 1144 守望者的逃离 2007年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 恶 ...
- 【Lintcode】120.Word Ladder
题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...
- ceph安装对象网关
1.概述 安装3个网关节点分别是:controller-03.controller-04和controller-05,使用ceph gw自带的Civetweb提供服务,前端使用nginx作为前端代理. ...
- js中关于事件捕获与事件冒泡的小实验
1.事件冒泡:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div - ...
- day1 java基础回顾-Junit单元测试
Junit单元测试框架的基本使用 一.搭建环境: 导入junit.jar包(junit4) 二.写测试类: 0,一般一个类对应一个测试类. 1,测试类与被测试类最好是放到同一个包中(可以是不同的源文件 ...
- Python绘制正态分布曲线
使用Python绘制正态分布曲线,借助matplotlib绘图工具: \[ f(x) = \dfrac{1}{\sqrt{2\pi}\sigma}\exp(-\dfrac{(x-\mu)^2}{2 ...