题意:背包重量为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. 修剪草坪 HYSBZ - 2442

    在一年前赢得了小镇的最佳草坪比赛后,FJ变得很懒,再也没有修剪过草坪.现在,新一轮的最佳草坪比赛又开始了,FJ希望能够再次夺冠. 然而,FJ的草坪非常脏乱,因此,FJ只能够让他的奶牛来完成这项工作.F ...

  2. greenplum 数组操作

    参考:http://gpdb.docs.pivotal.io/4390/admin_guide/query/topics/functions-operators.html Table 4. Advan ...

  3. python操作mongoDB(pymongo的使用)

    pymongo操作手册 连接数据库 方法一(推荐) import pymongo client = pymongo.MongoClient(host="localhost",por ...

  4. 解决Google浏览器不能打开kubernetes dashboard方法【转】

    在这片文章中,我将展示如何在Google Chrome上打开kubernetes dashboard.本文不叙述如何安装搭建docker和kubernetes,有关详情请上网查阅! 很多小伙伴们在自己 ...

  5. eos 智能合约开发体验

    eos编译安装 eos 特性 数据存储 eos投票智能合约开发 eos投票智能合约部署测试 注意避坑 eos编译安装 ERROR: Could not find a package configura ...

  6. HiBench成长笔记——(11) 分析源码run.sh

    #!/bin/bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor licen ...

  7. Unix-Time

    1. Unix_time 2. Year_2000_problem 3. Year_10,000_problem 4. Year_2038_problem 5. Time_formatting_and ...

  8. 字符串题汇总(python3)

    1.最小编辑距离 假设有两个字符串s1和s2,计算通过增添.删除.替换三种操作后,从s1转变为s2所需要的操作次数. #coding=utf-8 class Solution: def editDis ...

  9. Day4 - H - Following Orders POJ - 1270

    Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma stat ...

  10. 014.Delphi插件之QPlugins,MDI窗口

    不知道为什么,这个DEMO编译出来报错,运行不了,在QDAC群里问了一下也没人响应. 效果如下 主程序代码如下 unit Frm_Main; interface uses Winapi.Windows ...