如果可以每个物品拿多件,则从小到大遍历,否则从大到小遍历。

G - Piggy-Bank

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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<bits/stdc++.h>
using namespace std;
struct Node
{
int pan,wei;
}coin[];
int dp[];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int e,f;
scanf("%d%d",&e,&f);
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&coin[i].pan,&coin[i].wei);
}
for(int i=;i<;i++)
dp[i]=;
//cout<<dp[1003];
dp[]=;
for(int i=;i<n;i++)
{
for(int j=coin[i].wei;j<=f-e;j++)
dp[j]=min(dp[j],dp[j-coin[i].wei]+coin[i].pan);
}
if(dp[f-e]<)
{
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[f-e]);
}
else printf("This is impossible.\n");
}
}

装态转移方程:dp[j]=min(dp[j],dp[j-coni[i].wei]+coin[i].pan);dp[j]表示在重量为j的时候所能取得的最小的钱数。

poj 1114 完全背包 dp的更多相关文章

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

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

  2. POJ 1384 Piggy-Bank 背包DP

    所谓的全然背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 事实上和一般01背包没多少差别,只是数量能够无穷大,那么就能够利用一个物品累加到总容量结尾就能够了. ...

  3. POJ 2486 树形背包DP Apple Tree

    设d(u, j, 0)表示在以u为根的子树中至多走k步并且最终返回u,能吃到的最多的苹果. 则有状态转移方程: #include <iostream> #include <cstdi ...

  4. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  5. HDU 1114 Piggy-Bank 完全背包 dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1114 完全背包的题目,要求输出最小价值.然后一定要把给出的背包重量全部用完. 就是问一个背包为k的大小,n件物品 ...

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

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

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

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

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

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

  9. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

随机推荐

  1. Linux下远程cp命令scp

    2014-2.19  PS1.在用此命令cpLinux与Linux之间的数据时发现有些服务器上默认没有安装scp但用yum -y install scp提示么有这样的包 后来发现原来scp工具的安装包 ...

  2. 昂贵的聘礼(dijkstra)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38549   Accepted: 11158 Descripti ...

  3. 从客户端中检测到有潜在危险的 Request.Form 值。

    使用富文本编辑器是经常会遇到这个问题,在MVC中解决方法很简单只要在对应的action上添加[ValidateInput(false)]即可

  4. php面试题之三——PHP语言基础(基础部分)

    三.PHP语言基础 1. strlen( )与 mb_strlen( )的作用分别是什么(新浪网技术部) strlen和mb_strlen都是用于获取字符串长度. strlen只针对单字节编码字符,也 ...

  5. DrawText

    该函数在指定的矩形里写入格式化的正文,根据指定的方法对正文格式化(扩展的制表符,字符对齐.折行等).   int DrawText(HDC hDC, // 设备描述表句柄 LPCTSTR lpStri ...

  6. Python: 什么是*args和**kwargs

    转自: http://blog.sina.com.cn/s/blog_65a8ab5d0101fglm.html http://blog.csdn.net/chenjinyu_tang/article ...

  7. linux下查看文件夹的大小

    du -sh du -sh dir_name/ du -sm * | sort -n //统计当前目录大小 并安大小排序 转自:http://www.jb51.net/LINUXjishu/77450 ...

  8. C# 代理/委托 Delegate

    本文转载自努力,努力,努力 1. 委托的定义:委托是函数的封装,它代表一"类"函数.他们都符合一定的签名:拥有相同的参数列表,返回值类型.同时,委托也可以看成是对函数的抽象,是函数 ...

  9. HTML5 manifest离线缓存

    一.基本概念 离线缓存是HTML5新引入的技术,能够让你的Web应用程序指定哪些文件可以缓存在本地,使得你的网络断开时依然可以通过本地的缓存来进行访问浏览. 二.使用方法 1. MIME type 声 ...

  10. putty mtputty 设置utf8编码

    2013年10月30日 10:02:36 先load你指定的ip 然后选择左侧目录中的windows->translation 再在右侧选择utf-8编码 选中后,点击左侧目录中的session ...