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. PHP PSR-3 日志接口规范 (中文版)

    日志接口规范 本文制定了日志类库的通用接口规范. 本规范的主要目的,是为了让日志类库以简单通用的方式,通过接收一个 Psr\Log\LoggerInterface 对象,来记录日志信息. 框架以及CM ...

  2. Windows安装TensorFlow-Docker Installation of TensorFlow on Windows

    TensorFlow是Google开发的进行Deep Learning的包,目前只是支持在Linux和OSX上运行.不过这个秋季或许就有支持Windows的版本出现了,那么对于使用Windows的开发 ...

  3. Ancient Printer(tire树)

    Ancient Printer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  4. 【Xamarin挖墙脚系列:开始使用Xamari4.0系列产品开发IOS】

    一直沉默在Xamarin3.0系列版本上,升级到4.0之后,感觉有些变化.还得适应下. 1 build.host  代理消失了,成了SSH客户端登录.所以,Mac设备需要打开运行远程登录. 2 在Wi ...

  5. ant的入门 配置与安装

    最近需要用ant来生成文件,java类.我才开始了解了这个工具.仔细看了一下,感觉这个小工具的强大功能. 博主也是初学者,在网上收集了资料,尝试了配置:感觉有些高手写得不错变引用之. 配置如下: 以上 ...

  6. CSS发光边框文本框效果

    7,166 次阅读 ‹ NSH Blog 网页设计 CSS发光边框文本框效果 或许你看过Safari浏览器下,任何输入框都会有一个发光的蓝色边框,这不单纯只是蓝色边框而已,其实包含了许多CSS3技巧知 ...

  7. windows7 spark单机环境搭建及pycharm访问spark

    windows7 spark单机环境搭建 follow this link how to run apache spark on windows7 pycharm 访问本机 spark 安装py4j ...

  8. Multiple outputs from T4 made easy – revisited » DamienG

    Multiple outputs from T4 made easy – revisited » DamienG Multiple outputs from T4 made easy – revisi ...

  9. Team Formation(思维)

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  10. Java 线程第三版 第八章 Thread与Collection Class 读书笔记

        JDK1.2引入最有争议性的改变是将集合类默觉得不是Thread安全性的. 一.Collection Class的概述 1. 具有Threadsafe 的Collection Class: j ...