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

Piggy-Bank

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

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.
分析:
完全背包问题,注意初始化:
1.要求恰好装满背包(恰好装满存钱罐)
memset(dp,0x3f,sizeof(dp));//无穷大
dp[0]=0;
2.找的是最小值,把max改为min
 dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
代码如下:
#include<bits/stdc++.h>
#define max_v 10005
using namespace std;
int v[max_v],w[max_v];
int dp[max_v];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int x1,x2,c;
scanf("%d %d",&x1,&x2);
c=abs(x1-x2);
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d %d",&v[i],&w[i]);
}
memset(dp,0x3f,sizeof(dp));//无穷大 //要求恰好装满背包
dp[]=;
for(int i=;i<n;i++)
{
for(int j=w[i];j<=c;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+v[i]);
}
}
if(dp[c]==dp[max_v-])
{
printf("This is impossible.\n");
}else
{
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[c]);
}
}
return ;
}
 

HDU 1114(没有变形的完全背包)的更多相关文章

  1. HDU 1114:Piggy-Bank(完全背包)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. hdu 1114需要装满的完全背包 重点是背包初始化的问题

    .,. 最近在看背包九讲 所以就刷了一下背包的题目 这道题目是一个典型的完全背包问题 而且要求满包 在这里 我就简单整理一下背包初始化问题吧 对于没有要求满包的问题 也就是背包可以不取满的问题 在背包 ...

  3. HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)

    HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...

  4. HDU 1114 完全背包 HDU 2191 多重背包

    HDU 1114 Piggy-Bank 完全背包问题. 想想我们01背包是逆序遍历是为了保证什么? 保证每件物品只有两种状态,取或者不取.那么正序遍历呢? 这不就正好满足完全背包的条件了吗 means ...

  5. Piggy-Bank(HDU 1114)背包的一些基本变形

    Piggy-Bank  HDU 1114 初始化的细节问题: 因为要求恰好装满!! 所以初始化要注意: 初始化时除了F[0]为0,其它F[1..V]均设为−∞. 又这个题目是求最小价值: 则就是初始化 ...

  6. HDU 1114 Piggy-Bank(一维背包)

    题目地址:HDU 1114 把dp[0]初始化为0,其它的初始化为INF.这样就能保证最后的结果一定是满的,即一定是从0慢慢的加上来的. 代码例如以下: #include <algorithm& ...

  7. hdu 1114 dp动规 Piggy-Bank

    Piggy-Bank Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S ...

  8. HDU 5234 Happy birthday --- 三维01背包

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  9. 怒刷DP之 HDU 1114

    Piggy-Bank Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S ...

随机推荐

  1. AE中IHookHelper的用法 来自http://blog.sina.com.cn/s/blog_6faf711d0100xs1x.html

    IHookHelper 主要在用在自定义类型于AE带的的ICommand或ITool等, 1.实例化IHookHelper 对象: IHookHelper m_hookHelper = new Hoo ...

  2. 云数据库Redis版256M双机热备款

    云数据库Redis版是兼容Redis协议标准的.提供持久化的缓存式数据库服务,基于高可靠双机热备架构:全新推出的256M小规格款,适用于高QPS.小数据量业务,并支持免费全量迁移,完美服务于个人开发者 ...

  3. PDW V2培训简记

    最近有幸参加了微软专家进行的为期一周PDW培训,将一些知识点记录如下: 不知道PDW是什么东西的,简单介绍一下:这是微软与HP/Dell合作推出的SQL Server数据仓库一体机,由HP或DELL提 ...

  4. OneNet平台初探成功

    1.经过半个月的研究,终于成功对接OneNet平台,实现远程控制LED灯的亮灭 2.在调试的过程中也遇到了很多问题,做一下总结 3.硬件:STM32F103C8T6的最小系统板,ESP8266-WiF ...

  5. Activity被回收导致fragment的getActivity为空

    在编写含有Fragment代码的时候,经常会遇到这种情况,假如app长时间在后台运行,再点击进入会crash,而且fragment页面有重叠的现象. 如果系统内存不足.或者切换横竖屏.或者app长时间 ...

  6. redis 概述、windows版本下载启动访问退出安装、中文乱码、RedisDesktopManager下载

    redis 概述 redis的key是string类型的:value有多种类型,但放入的不是特定类型数据,添加的都是string,只是redis把这些值组织成了各种数据结构.key和相应的值都是str ...

  7. 【Java】解析xml

    xml: <?xml version="1.0" encoding="GB2312"?> <RESULT> <VALUE> ...

  8. 传递命令行参数示例代码 (C 和 Python)

    C语言 在 C 语言中, 使用 main 函数的输入参数 argc 和 argv 传入命令行参数. argc 为 int 类型, 表示传入命令行参数的个数 (argument count); argv ...

  9. [WSUS] [Windows 10 Upgrade 1607/1703] 升级出错,出现 0xC1800118 或者卡在下载中…… 0%后失败

    1. 安装 KB3159706 ,并进行安装后维护操作:https://support.microsoft.com/en-us/help/3159706/update-enables-esd-decr ...

  10. JBoss jmx-console中的秘密

    JBoss jmx-console中的秘密 https://wenku.baidu.com/view/fe196f047cd184254b35351d.html