题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114

题目大意:完全背包问题,不过这次是求的最小值。

解题思路:首先是初始化问题,其次就是状态转移的时候,如果使用二维数组,那么应当是:

dp[i][j] = dp[i - 1][j]  j < w[i]

dp[i][j] = min(dp[i][j], dp[i][j - w[i]] + v[i])  j >= w[i]

一维的话直接从j = w[i]开始从前往后去min即可。

代码:

const int maxn = 5e2 + 5;
const int maxm = 1e4 + 5;
int e, f, n;
struct money{int v, w;
};
money a[maxn];
int dp[maxm];int solve(){f = f - e;memset(dp, 0x3f, sizeof(dp));dp[0] = 0;for(int i = 1; i <= n; i++){for(int j = a[i].w; j <= f; j++){dp[j] = min(dp[j], dp[j - a[i].w] + a[i].v);}}if(dp[f] >= inf) return -1;return dp[f];
}
int main(){int T;scanf("%d", &T);while(T--){scanf("%d %d %d", &e, &f, &n);for(int i = 1; i <= n; i++) scanf("%d %d", &a[i].v, &a[i].w);int ans = solve();if(ans == -1) printf("This is impossible.\n");else printf("The minimum amount of money in the piggy-bank is %d.\n", ans);}

题目:

Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26869    Accepted Submission(s): 13590

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.

转载于:https://www.cnblogs.com/bolderic/p/7365519.html

HDU 1114 Piggy-Bank 简单DP相关推荐

  1. hdu 1559 最大子矩阵 (简单dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1559 1 #include <cstring> 2 #include <cstdlib& ...

  2. 这段时间做的简单dp题目(部分)

    这些时间vj上做的部分题目 HDU5115 题意:第一行t,t组测试数据,每组数据第一行输入n表示n匹狼,第二行给出一个序列表示每匹狼的伤害,第三行给出每匹狼能给周围狼的伤害增幅,求怎样打可以得到最小 ...

  3. HDU 5375 Gray code (简单dp)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Ot ...

  4. hdu 2881(简单dp)

     题意:n*n的矩阵,里面有m个格子是有任务要去完成的,t,x,y表示要在第t秒到达(x,y)的格子完成任务,问你最多可以完成多少 解题思路:简单dp,将时间排个序后就是LIS #include< ...

  5. HDU 1158【简单dp】

    题意:给你一个项目,需要几个月来完成买,同时也给你每个月最少需要的工人数.并且告诉你hiring,firing每个工人的钱数,以及每个月应付每个工人的工资.求项目完成时最小花费. 这是个简单dp,思路 ...

  6. hdu 3016 Man Down(简单线段树简单DP)

    Man Down Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. NUC-ACM/ICPC 寒假训练 简单DP A - G题

    第一题:数塔 HDU - 2084 做法: 从第 i , j 个 节点往下走的最优解可以由从第 i+1,j 个节点往下走的最优解和第i+1,j+1个节点往下走的最优解得出,二者取其优即可. 代码: 记 ...

  8. D - 猪钱罐 HDU - 1114

    D - 猪钱罐 HDU - 1114 在 ACM 能够开展之前,必须准备预算,并获得必要的财力支持.该活动的主要收入来自于 Irreversibly Bound Money (IBM).思路很简单.任 ...

  9. 4.15 每周作业 —— 简单DP

    免费馅饼 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  10. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

最新文章

  1. Kube-Scheduler插件的自定义
  2. 一段关于Unix与 Linux的暗黑史
  3. IDEA中无法识别servlet类或找不到javax.servlet.*
  4. 炫酷!用Python制作漂亮的流动桑基图
  5. (三)docker-compose 启动 Redis 服务
  6. react滑动切换tab动画效果_[React Native]react-native-scrollable-tab-view(入门篇)
  7. 《朝花夕拾》金句摘抄(二)
  8. httpd mysql认证_Apache Httpd服务器之认证与授权
  9. 用html和css做动态动物,CSS3 SVG实现可爱的动物哈士奇和狐狸动画
  10. 端口映射内网穿透——网络通
  11. 项目Beta冲刺(4/7)(追光的人)(2019.5.26)
  12. 精细化用电侧能源管控 解码光伏电站运维痛点
  13. c语言扑克牌同花顺比大小,为什么打扑克时“同花顺”最大
  14. 基于STM32设计的健康检测设备(测温心率计步)
  15. 天池广东工业智造大数据创新大赛--铝型材表面瑕疵识别 --top1方案
  16. 关于matlab中矩阵的运算
  17. 羽化 matlab,MATLAB实现图像羽化处理(图像羽化处理)
  18. 华为云设计语言_让开发者相见恨晚?!华为云软件开发云实现云上敏捷开发
  19. x=1u c语言,c语言中1u是什么意思
  20. 修复因更新iCloud设置而卡住的iPhone / iPad的六种方法

热门文章

  1. Git error. Command: `git ls-files --cached --exclude-standard --recurse-submodules`
  2. vue中webpack编译打包使用之Vue知识点归纳(十一)
  3. Hiernate概述
  4. java基础-java语言中的关键字总结
  5. Linux之强大的selinux
  6. Python中表达式和语句及for、while循环练习
  7. Jmeter接口测试使用beanshell断言json返回
  8. golang(2):beego 环境搭建
  9. python中实现多线程的几种方式
  10. 打造一个全命令行的Android构建系统