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 < ...
随机推荐
- 改变Ecplise项目窗口字体样式
Eclipse\plugins\org.eclipse.ui.themes_1.1.1.v20151026-1355\css e4-dark_win.css CTabFolder Tree, CTab ...
- codeforces 587B B. Duff in Beach(dp)
题目链接: B. Duff in Beach time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- hyperledger fabric学习(1)
第一部分 环境搭建 说明 本次环境搭建是是现在虚拟机中,采用ubuntu 16.04版本,安装多次成功. 首先安装一些常用的工具 sudo apt-get update sudo apt-get in ...
- 【Lintcode】095.Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- finetune
微调的具体方法和技巧有很多种,这里总结了在不同场景下的微调技巧: 1)新数据集比较小且和原数据集相似.因为新数据集比较小(比如<5000),如果fine-tune可能会过拟合:又因为新旧数据集类 ...
- poj3254二进制放牛——状态压缩DP
题目:http://poj.org/problem?id=3254 利用二进制压缩状态,每一个整数代表一行的01情况: 注意预处理出二进制表示下没有两个1相邻的数的方法,我的方法(不知为何)错了,看到 ...
- 向nexus远程仓库里面添加JAR
向nexus远程仓库里面添加JAR 远程仓库:http://10.1.252.21:8081/nexus/index.html admin/admin123 方法一:手动 在左侧选择:Reposito ...
- linux——boot空间不足
1. 先用df命令,查看磁盘分区情况 2. dpkg --get-selections|grep linux-image(查看更新了多少内核) root@ubuntu:/home/hadoop# dp ...
- ReSIProcate环境搭建
1首先下载resiprocate-1.6 2取消resiprocate-1.6目录的只读属性 3然后使用Visual Studio 2008打开resiprocate-1.6下的reSIProcate ...
- linux中syslog自定义存储路径的方法
方法一: 1. su //切换到root用户下2. cp /etc/sysconfig/rsyslog /etc/sysconfig/rsyslogbak //备份vim /etc/sysconfi ...