题意:背包重量为F-E,有N种硬币,价值为Pi,重量为Wi,硬币个数enough(无穷多个),问若要将背包完全塞满,最少需要多少钱,若塞不满输出“This is impossible.”。

分析:完全背包。

(1)构造二维数组:

dp[i][j]---背包重量为j时,前i种物品可得到的最大价值。

dp[i][j]=max{dp[i-1][j-k*W[i]]+k*P[i]|0<=k*W[i]<=F-E}

(2)构造滚动数组:(一维)

dp[j]---背包重量为j时,当前状态可得到的最大价值。

dp[j] = max(dp[j], dp[j - W[i]] + P[i]);(顺序枚举0~F-E)

原因:很显然,由于当前物品无穷多个,所以dp[j]的更新依赖于当前物品的dp[j - W[i]]的更新。

例如,背包重量为10。对于第一个物品,重量为3,价值为5,则dp[3]=5,而更新dp[6]时,dp[6]=max(dp[6],dp[6-3]+5)=10,已更新过的dp[3]相当于取一件第一个物品,因此dp[6]只需转移dp[3]的即可达到取两件第一个物品的目的。

对于本题,求最小值,因此dp[j] = min(dp[j], dp[j - W[i]] + P[i]);

又因背包要完全塞满,因此dp[0] = 0;这样可保证只有能将当前背包完全塞满的重量才可被更新为非INF。

还是上述例子,在研究装入第一件物品时,当更新dp[4]时,因为重量为3,显然只装第一件物品是塞不满重量为4的背包,所以dp[4]暂时不能被更新。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 500 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int P[MAXN], W[MAXN], dp[MAXT];
int main(){
int T;
scanf("%d", &T);
while(T--){
int E, F;
scanf("%d%d", &E, &F);
int w = F - E;
int N;
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%d%d", &P[i], &W[i]);
}
memset(dp, INT_INF, sizeof dp);
dp[0] = 0;
for(int i = 0; i < N; ++i){
for(int j = W[i]; j <= w; ++j){
dp[j] = min(dp[j], dp[j - W[i]] + P[i]);
}
}
if(dp[w] == INT_INF) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[w]);
}
return 0;
}

  

HDU - 1114 Piggy-Bank(完全背包讲解)的更多相关文章

  1. HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)

    HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...

  2. HDU 1114 Piggy-Bank(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 题目大意:根据储钱罐的重量,求出里面钱最少有多少.给定储钱罐的初始重量,装硬币后重量,和每个对应 ...

  3. HDU - 1114 Piggy-Bank 【完全背包】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意 给出一个储钱罐 不知道里面有多少钱 但是可以通过重量来判断 先给出空储钱罐的重量 再给出装 ...

  4. 题解报告:hdu 1114 Piggy-Bank(完全背包恰好装满)

    Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...

  5. hdu(1114)——Piggy-Bank(全然背包)

    唔..近期在练基础dp 这道题挺简单的(haha).可是我仅仅想说这里得注意一个细节. 首先题意: 有T组例子,然后给出储蓄罐的起始重量E,结束重量F(也就是当它里面存满了零钱的时候).然后给你一个数 ...

  6. HDU 1114 Piggy-Bank ——(完全背包)

    差不多是一个裸的完全背包,只是要求满容量的最小值而已.那么dp值全部初始化为inf,并且初始化一下dp[0]即可.代码如下: #include <stdio.h> #include < ...

  7. HDU 1114 完全背包 HDU 2191 多重背包

    HDU 1114 Piggy-Bank 完全背包问题. 想想我们01背包是逆序遍历是为了保证什么? 保证每件物品只有两种状态,取或者不取.那么正序遍历呢? 这不就正好满足完全背包的条件了吗 means ...

  8. Piggy-Bank(HDU 1114)背包的一些基本变形

    Piggy-Bank  HDU 1114 初始化的细节问题: 因为要求恰好装满!! 所以初始化要注意: 初始化时除了F[0]为0,其它F[1..V]均设为−∞. 又这个题目是求最小价值: 则就是初始化 ...

  9. HDU 1114 Piggy-Bank(一维背包)

    题目地址:HDU 1114 把dp[0]初始化为0,其它的初始化为INF.这样就能保证最后的结果一定是满的,即一定是从0慢慢的加上来的. 代码例如以下: #include <algorithm& ...

随机推荐

  1. pytorch 调整tensor的维度位置

    target.permute([0, 3, 1, 2]) 一定要使用permute以及中括号 一些在我这里没起到作用的网上的例子: 1. https://blog.csdn.net/zouxiaolv ...

  2. Windows Server 2008 R2 SP1 中IIS7.5 和 TOMCAT7 整合笔记

    Windows Server 2008 R2 SP1 中IIS7.5 和 TOMCAT7 整合笔记 来源:www.roak.com 整合文件在百度网盘或博客盘 配置了N次,64位操作系统真坑爹~~~下 ...

  3. mabatis--使用mapper代理开发dao

    1.编写mapper.xml映射文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE m ...

  4. [易语言][ExDui][Tutorial]0.Hello,world!

    原创博客,请勿在未经授权的情况下转载. At the Beginning... 由于近期爱好摸鱼,并且对Gui开发萌生一丝兴趣.在尝试WPF被虐,使用Qt却不太喜欢整套庞大的框架后-- I choos ...

  5. iOS Burp suite CA证书 HTTPS

    设置好burp suite代理后,在浏览器地址输入http://burp/,下载CA证书: 在iOS上下载CA证书,可通过邮件或百度云等一切iOS可以访问证书文件的方法: 点击证书文件iOS提示安装, ...

  6. 新闻网大数据实时分析可视化系统项目——18、Spark SQL快速离线数据分析

    1.Spark SQL概述 1)Spark SQL是Spark核心功能的一部分,是在2014年4月份Spark1.0版本时发布的. 2)Spark SQL可以直接运行SQL或者HiveQL语句 3)B ...

  7. docker 为镜像添加ssh服务-docker commit命令创建

    环境centos7 一.准备工作 docker pull ubuntu:18.04 docker run -it ubuntu:18.04 bash 二.配置软件源apt-get update,如果系 ...

  8. 真香的flex弹性布局

    如何实现一个左中右的布局 在flex出现之前 #box{ color: white; } #left{ float: left; width: 30%; background-color: red; ...

  9. Day8 - D - Multiplication Table CodeForces - 448D

    Bizon the Champion isn't just charming, he also is very smart. While some of us were learning the mu ...

  10. Day 4 -E - Catenyms POJ - 2337

    A catenym is a pair of words separated by a period such that the last letter of the first word is th ...