所谓的全然背包,就是说物品没有限制数量的。

怎么起个这么intimidating(吓人)的名字?

事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了。

本题要求装满的,故此添加个限制就能够了。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. inline int min(int a, int b) { return a < b? a : b; }
  5.  
  6. const int MAX_W = 10001;
  7. const int MAX_N = 501;
  8. int val[MAX_N] = {0};
  9. int wei[MAX_N] = {0};
  10. int tbl[MAX_W];
  11.  
  12. int bagDP(int N, int W)
  13. {
  14. for (int i = 0; i <= W; i++) tbl[i] = 0;
  15.  
  16. for (int j = wei[1]; j <= W; j += wei[1])
  17. tbl[j] = val[1] + tbl[j-wei[1]];
  18.  
  19. for (int i = 2; i <= N; i++)
  20. {
  21. for (int j = wei[i]; j <= W; j++)
  22. {
  23. if (j-wei[i]==0 || tbl[j-wei[i]])
  24. {
  25. if (tbl[j]) tbl[j] = min(tbl[j],tbl[j-wei[i]]+val[i]);
  26. else tbl[j] = tbl[j-wei[i]]+val[i];
  27. }
  28. }
  29. }
  30. return tbl[W];
  31. }
  32.  
  33. int main()
  34. {
  35. int E, F;//weight of an empty pig and of the pig filled with coins
  36. int T, N;// P:value, W: weight
  37. scanf("%d", &T);
  38. while (T--)
  39. {
  40. scanf("%d %d %d", &E, &F, &N);
  41. for (int i = 1; i <= N; i++)
  42. {
  43. scanf("%d %d", &val[i], &wei[i]);
  44. }
  45. int ans = bagDP(N, F-E);
  46. if (ans) printf("The minimum amount of money in the piggy-bank is %d.\n", ans);
  47. else puts("This is impossible.");
  48. }
  49. return 0;
  50. }

POJ 1384 Piggy-Bank 背包DP的更多相关文章

  1. POJ 1384 POJ 1384 Piggy-Bank(全然背包)

    链接:http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...

  2. poj 3624 Charm Bracelet 背包DP

    Charm Bracelet Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3624 Descripti ...

  3. poj 2229 【完全背包dp】【递推dp】

    poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 828 ...

  4. POJ 1384 Piggy-Bank(完全背包)

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

  5. POJ 1384【完全背包】

    题意: 已知储蓄罐满时的质量f以及空时质量e, 有n种硬币,每种硬币的价值为p,质量为w, 求该储蓄罐中的最少有多少钱? 思路: 完全背包思想,问题是在一个重量下的最小价值 那么只要变一下符号就好了? ...

  6. POJ 1155 树形背包(DP) TELE

    题目链接:  POJ 1155 TELE 分析:  用dp[i][j]表示在结点i下最j个用户公司的收益, 做为背包处理.        dp[cnt][i+j] = max( dp[cnt][i+j ...

  7. 【bzoj1531】[POI2005]Bank notes 多重背包dp

    题目描述 Byteotian Bit Bank (BBB) 拥有一套先进的货币系统,这个系统一共有n种面值的硬币,面值分别为b1, b2,..., bn. 但是每种硬币有数量限制,现在我们想要凑出面值 ...

  8. poj 1417 True Liars(并查集+背包dp)

    题目链接:http://poj.org/problem?id=1417 题意:就是给出n个问题有p1个好人,p2个坏人,问x,y是否是同类人,坏人只会说谎话,好人只会说实话. 最后问能否得出全部的好人 ...

  9. poj 2184 01背包变形【背包dp】

    POJ 2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14657   Accepte ...

  10. poj1417(带权并查集+背包DP+路径回溯)

    题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...

随机推荐

  1. [置顶] iframe使用总结(实战)

    说在前面的话,iframe是可以做很多事情的. 例如: a>通过iframe实现跨域; b>使用iframe解决IE6下select遮挡不住的问题 c>通过iframe解决Ajax的 ...

  2. Python眼睛护士改进版

    添加了设定从(0,0)显示:self.root.geometry('1000x200+0+0')其实主要是两个0.那个1000和200是没用的,因为已经设定了minsize. 添加了窗口置顶:self ...

  3. 处理json中影响解析的多余引號

    在xml中,敏感字符是尖括号,在json中,敏感字符是引號,上文中我们介绍了怎样处理xml中的敏感字符.本文说说怎样处理json中的敏感字符. 思路与上文同样,不再赘述.直接上代码: json–> ...

  4. TP 控制器扩展_initialize方法实现原理

    参考网址:http://gongwen.sinaapp.com/article-59.html 控制器扩展接口 系统Action类提供了一个初始化方法_initialize接口,可以用于扩展需要,_i ...

  5. SWT的文件选择对话框I的使用

    swt文件选择框 FileDialog fileselect=new FileDialog(shell,SWT.SINGLE);                          fileselect ...

  6. ibatis dynamic的用法

    <select id="idididid" resultClass="java.uitl.HashMap" parameterClass="ja ...

  7. discuz 插件开发 新手入门

    作为一个新手,目前也是刚刚玩转discuz的插件功能,好东西不敢独享,就拿出来大家一起分享入门的过程.现在网上很多关于discuz的插件教程都是很简单的教程,原因可能是这个东西是商业化的东西,本着分享 ...

  8. http协议之cookie标准RFC6265介绍

      [Docs] [txt|pdf] [draft-ietf-httpst...] [Diff1] [Diff2] [Errata] PROPOSED STANDARD Errata Exist In ...

  9. Troubleshooting &quot;Global Enqueue Services Deadlock detected&quot; (Doc ID 1443482.1)

    In this Document   _afrLoop=1021148011984950&id=1443482.1&displayIndex=1&_afrWindowMode= ...

  10. sgu 286. Ancient decoration(最小环覆盖)

    给你一个n个点,每个点度为k(k为偶数)的无向图,问是否能将图中的n条边染色,使得每个点都拥有两条被染色的边.也就是说,是否存在拥有原图中n条边的子图,使得每个点的度为2?仔细想想,每个点的度为2,实 ...