Piggy-Bank

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.
 
题目描述:给的存钱罐中钱币的总质量,一直其中的每种硬币的价值和质量,求出可能的存钱罐中最小的价值,状态转移方程p[j]=min{p[j],p[j-w[j]]+v[i]} 
p[j]表示在总质量为j时的最小价值(但此时取得的最小价值的总质量必须等于存钱罐中的钱币总质量) 第一次做dp背包题,还不是很理解,回头还要认真看看
 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int E,F,W;
int v[],w[],p[];
int n;
void dp()
{
int i,j;
for(i=;i<=n;i++)
for(j=w[i];j<=W;j++)
p[j]=min(p[j],p[j-w[i]]+v[i]);
}
int main()
{
int i,j;
int T;
cin>>T;
while(T--)
{
cin>>E>>F;
W=F-E;
cin>>n;
for(i=;i<=W;i++)
p[i]=;
for(i=;i<=n;i++)
cin>>v[i]>>w[i];
p[]=;
dp();
if(p[W]==)
cout<<"This is impossible."<<endl;
else
cout<<"The minimum amout of money in the piggy-bank is "<<p[W]<<"."<<endl;
}
return ;
}

Piggy-Bank (hdoj1114)的更多相关文章

  1. PAT 1017 Queueing at Bank (模拟)

    1017. Queueing at Bank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Supp ...

  2. JZ2440开发笔记(6)——存储控制器

    存储控制器与CPU及其它外设的关系 我们看到cpu上集成了一个存储管理器,外围的存储设备都接在这个存储管理器上.cpu负责发出命令,其它的一切工作都交给了存储管理器.那么存储管理器是如何来管理这些外设 ...

  3. Web安全相关(二):跨站请求伪造(CSRF/XSRF)

    简介 CSRF(Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对 ...

  4. java从基础知识(十)java多线程(下)

    首先介绍可见性.原子性.有序性.重排序这几个概念 原子性:即一个操作或多个操作要么全部执行并且执行的过程不会被任何因素打断,要么都不执行. 可见性:一个线程对共享变量值的修改,能够及时地被其它线程看到 ...

  5. Device Tree(二):基本概念

    转自:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制是用 ...

  6. CC2530使用串口下载(SBL)

    工作环境: WIN7 64位 IAR 版本: 8.10.3 (8.10.3.10338) ZStack-CC2530-2.3.1-1.4.0协议栈,下载地址:http://download.csdn. ...

  7. Google Map API V3开发(6) 代码

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

  8. (三)内存 SDRAM 驱动实验 (杨铸 130 页)(勉强能懂个大概)

    SDRAM 芯片讲解: 地址: 行地址 (A0-A12) 列地址 (A0-A8)    片选信号(BA0 BA1)(L-BANK)(因为SDRAM有 4片) 两片SDRAM 连线唯一区别在 UDQM ...

  9. LTE 测试文档(翻译)

    Testing Documentation 翻译 (如有不当的地方,欢迎指正!)     1 概述   为了测试和验证 ns-3 LTE 模块,文档提供了几个 test suites (集成在 ns- ...

随机推荐

  1. 10--动作系统(四)动作类中的reverse方法

    上一篇文章在使用持续动作过程中遇到不少问题,以获取动作类的反系动作尤为突出.所以今天把动作类找了个遍,先将大部分动作类是否实现reverse方法总结如下: T表示实现F表示没有实现. 观察可以发现带T ...

  2. project euler 25 fibonacci

    数学方法: Saying that a number contains 1000 digits is the same as saying that it's greater than 10**999 ...

  3. [TYVJ] P1010 笨小猴

    笨小猴 背景 Background NOIP2008复赛提高组第一题   描述 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种 ...

  4. C语言必背18个经典程序

    C语言必背18个经典程序 1./*输出9*9口诀.共9行9列,i控制行,j控制列.*/ #include "stdio.h" main() {int i,j,result; for ...

  5. Mac OS X用户,使用homebrew安装,FreeBSD也可以

    qtkeychain 这是编译和运行软件必须的库.各平台都可以编译安装.对于Mac OS X用户,使用homebrew安装: brew install qt5keychain (旧版本的Mac OS ...

  6. css案例学习之div ul li a 实现导航效果

    效果 代码 <html> <head> <title>无需表格的菜单</title> <style> body{ background-co ...

  7. svn local delete, incoming delete upon update 解决办法

    经常有人会说,树冲突是很难解决的一类冲突,其实一旦了解了其原理,要解决也不难.先回顾下对于树冲突的定义.     树冲突:当一名开发人员移动.重命名.删除一个文件或文件夹,而另一名开发人员也对它们进行 ...

  8. SSCTF Final PWN

    比赛过去了两个月了,抽出时间,将当时的PWN给总结一下. 和线上塞的题的背景一样,只不过洞不一样了.Checksec一样,发现各种防护措施都开了. 程序模拟了简单的堆的管理,以及cookie的保护机制 ...

  9. stagefright框架(六)-Audio Playback的流程

    到目前为止,我们都只着重在video处理的部分,对于audio却只字未提.这篇文章将会开始audio处理的流程. Stagefright中关于audio的部分是交由AudioPlayer来处理,它是在 ...

  10. stagefright框架(三)-选择Video Decode

    在<Stagefright (1) – Video Playback的流程>中,我们并没有详述Stagefright是如何根据影片档的类型来选择适合的video decoder,现在,就让 ...