解题心得:
1、这是一个完全背包问题的变形,题目要求是求在规定的重量下求价值最小,所以需要将d[0]=0关键的初始化
2、当不可能出现最小的价值时,d的状态并没有被改变,说明并没有放进去一个硬币。

题目:

题目解释:
输入一个空存钱罐的质量,再规定一个存钱罐的存满的质量,告诉你几个硬币的质量和价值,求最小的价值和。

Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 24389    Accepted Submission(s): 12367


Problem Description
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea
behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there
should be enough cash in the piggy-bank to pay everything that needs to be paid.


But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility
is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank
that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
 

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate
the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives
the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's
weight in grams.
 

Output
Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that
can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".
 

Sample Input

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
 

Sample Output

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std; const int INF = 0x7ffffff;//可以这样规定最大值
struct t
{
int we;
int va;
} ty[510];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int e,f;
scanf("%d%d",&e,&f);
int W = f - e;
int d[10100];
int n;
scanf("%d",&n);
for(int i=1;i<=W;i++)
d[i] = INF;
d[0] = 0;//很关键的初始化
for(int i=0; i<n; i++)
{
scanf("%d%d",&ty[i].va,&ty[i].we);
for(int j=ty[i].we;j<=W;j++)
{
if(d[j] > d[j-ty[i].we] + ty[i].va)
d[j] = d[j-ty[i].we] + ty[i].va;
}
}
if(d[W] == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",d[W]);
}
return 0;
}

动态规划:完全背包问题-HDU1114-Piggy-Bank的更多相关文章

  1. [Dynamic Programming]动态规划之背包问题

    动态规划之背包问题 例题 现有4样物品n = ['a', 'b', 'c', 'd'],重量分别为w = [2, 4, 5, 3],价值分别为v = [5, 4, 6, 2].背包最大承重c = 9. ...

  2. 记录结果再利用的"动态规划"之背包问题

    参考<挑战程序设计竞赛>p51 https://www.cnblogs.com/Ymir-TaoMee/p/9419377.html 01背包问题 问题描述:有n个重量和价值分别为wi.v ...

  3. 动态规划_01背包问题_Java实现

    原文地址:http://blog.csdn.net/ljmingcom304/article/details/50328141 本文出自:[梁敬明的博客] 1.动态规划 什么是动态规划?动态规划就是将 ...

  4. 【动态规划/多重背包问题】POJ1014-Dividing

    多重背包问题的优化版来做,详见之前的动态规划读书笔记. dp[i][j]表示前i中数加得到j时第i种数最多剩余几个(不能加和得到i的情况下为-1)递推式为: dp[i][j]=mi(dp[i-1][j ...

  5. 【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】

    任务说明:这是最基础的动态规划.不过如果是第一次接触会有些难以理解.加油闯过这个坎. 01背包二维数组优化成滚动数组的时候有坑有坑有坑!!!必须要downto,downto,downto 情景和代码见 ...

  6. python 动态规划(背包问题和最长公共子串)

    背包问题 现在要往一个可以装4个单位重量的背包里怎么装价值最高:A重量1个单位,价值15:B重量3个单位,价值20:C重量4个重量,价值30 使用动态规划填充空格 class SolutionBag: ...

  7. poj 1742 Coins (动态规划,背包问题)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32977   Accepted: 11208 Descripti ...

  8. 九度OJ 1455 珍惜现在,感恩生活 -- 动态规划(背包问题)

    题目地址:http://ac.jobdu.com/problem.php?pid=1455 题目描述: 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元, ...

  9. 记忆搜索与动态规划——DP背包问题

    题目描述 01背包问题 有n个重量和价值分别为\(w_i,v_i\)的物品.从这些物品中挑选出总重量不超过W的物品,求所有挑选方案中价值中总和的最大值. 限制条件 1 <= n <= 10 ...

  10. 用js实现动态规划解决背包问题

    动态规划的原理: 移至到该同学的博文中,讲解的声动易懂 https://www.jianshu.com/p/a66d5ce49df5 现在主要是用js来实现动态规划 function bb(v, w, ...

随机推荐

  1. Quality of Service (QoS) in LTE

    Background: Why we need QoS ? There are premium subscribers who always want to have better user expe ...

  2. JavaScript函数体系

    第4章  JavaScript函数 1. 函数基本介绍 ① 为什么需要函数 函数最大的好处就是将零散的代码封装到了一起,当我们要再次使用该功能的时候,不需要再重新书写代码,只需要调用封装好的函数就可以 ...

  3. 性能测试学习第十一天_Analysis

    Analysis的功能:对测试运行结果进行查看,分析和比较 导入分析文件:loadrunner results文件和analysis session文件 Analysis窗口: 1.会话浏览器窗格 2 ...

  4. 两道sql面试题

    两道sql面试题:    1. 数据库表A的数据如下:             year   quarter             2001      1             2001      ...

  5. 微信退款和支付宝退款接口调用(java版)

    项目中需要使用到微信和支付宝的退款功能,在这两天研究了一下这两个平台的退款,有很多坑,在开发中需要留意 1.微信退款接口 相对来说我感觉微信的退款接口还是比较好调用的,直接发送httppost请求即可 ...

  6. 关于wav文件fft处理后x,y轴坐标数据的问题

    1.关于横坐标的频率的最大值是采样频率,那么每个点对应的频率值就很好算了:f(n) = [Fs/(N/2)]*n  (Fs是采样频率,常见的是44.1KHz(44100),N是采样点数,k表是第k个点 ...

  7. unicode字符和多字节字符的相互转换接口

    作者:朱金灿 来源:http://blog.csdn.net/clever101 发现开源代码的可利用资源真多,从sqlite3的源码中抠出了几个字符转换接口,稍微改造下了发现还挺好用的.下面是实现代 ...

  8. visual studio2010中C#生成的,ArcGIS二次开发的basetool的dll,注册为COM组件tlb文件,并在arcmap中加载使用

    写了个标题好长啊~~~~ 这两天又认识了一个新玩意,记录一下下,啦啦啦~~~~~ 话说,认识arcgis快十年了,从桌面版到engine的二次开发,其实不过才认识到它的冰山一角, 它总是能带来很多还未 ...

  9. PIC IDE编译器变量问题

    1.用const关键字是不能把变量定义到ROM区域的,在IDE编译器里要在变量的定义前面加入rom关键字.例如: rom char tmp[257]={0};const rom char tmp[25 ...

  10. 查看mysql表和数据库的大小

    转自:http://xiaosu.blog.51cto.com/2914416/687835 1.查看数据库的大小 use 数据库名SELECT sum(DATA_LENGTH)+sum(INDEX_ ...