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 < ...
随机推荐
- VC Q&A (原创)
Q1:External Dependencies有什么作用? A1:(网友答复:)External Dependencies是说你没有把这个文件加入到这个工程中,但是需要这个文件的支持.当然有时是 ...
- 递归------python实现列表创建二叉树
# -*- coding:utf-8 -*- '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None ...
- java定时器,留着用
说明:该定时器作用是 设定定时器首次执行的时间firstTime和执行间隔period,如firstTime=2015-3-25 9:00:00,period=24小时,若程序启动时,已经超过firs ...
- 洛谷 P4238 [模板] 多项式求逆
题目:https://www.luogu.org/problemnew/show/P4238 看博客:https://www.cnblogs.com/xiefengze1/p/9107752.html ...
- 如何让Surface RT支持网站的flash
Go to the desktop version of IE10, hit the click ALT button on your keyboard, click on and then Comp ...
- python json ajax django四星聚会
什么是json: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programm ...
- Scala学习——操作符(初)
经常看到却反应不出来的(->) val a = 2 val b = a->4 //表示生成一个tuple println(b._1+" "b._2) //2 4
- 把myeclipse中的web项目导入eclipse中不能编程web项目的解决办法
title: 把myeclipse中的web项目导入eclipse中不能编程web项目的解决办法 tags: grammar_cjkRuby: true --- 右键单击项目,properties-- ...
- Linux的学习思路
自学嵌入式确实不大现实(当然也不是说没有这个可能),毕竟嵌入式难度也是比较大的. 嵌入式的应用主要是几个方向, 一是系统开发:侧重开发环境搭建.内核原理.交叉编译等: 二是嵌入式Linux应用开发:侧 ...
- Spring管理Filter和Servlet(在servlet中注入spring容器中的bean)
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建.如果要在servlet中使用spring容器管理业务对 ...