题目链接: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. vue三要素及底层实现机制

    深入解析Vue 我们首先来熟悉一下我们这个文档所学习内容的流程. 先对比一下jQuery和Vue的区别,再讲述Vue的MVVM模型,接着讲解Vue的实现流程. 当然,我是不相信没有对比哪来的伤害,没有 ...

  2. 简单侧边栏js效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 润乾配置连接kingbase(金仓)数据库

     问题背景 客户根据项目的不同,使用润乾连接的数据库类型各种各样,此文针对前几日使用润乾设计器连接kingbase金仓数据库做一个说明. kingbase金仓数据库是一款国产数据库,操作方式和配置 ...

  4. cookie implements session

    cookie实现会话 服务器调用response.addCookie()设置set-cookie响应头后,浏览器收到这个响应头与数值后,会将它以文件的形式存储于本地PC上.当浏览器再次访问同一Web服 ...

  5. Pig sample用法举例

    some = sample data 0.1   遍历整个数据集,获取指定比例的行数的数据,获取的数据不确定,条数也不准确.   内部重写为filter data by random() <= ...

  6. redis介绍(7)高级用法

    redis的过期策略以及内存淘汰机制 分析:这个问题其实相当重要,到底redis有没用到家,这个问题就可以看出来.比如你redis只能存5G数据,可是你写了10G,那会删5G的数据.怎么删的,这个问题 ...

  7. 机器学习之step by step实战及知识积累笔记

    数据工作者工作时间划分 据crowdflower数据科学研究报告,数据科学工作者的时间分配主要在以下几个领域: 首先是数据收集要占20%左右的时间和精力,接着就是数据清洗和再组织需要占用60%的时间. ...

  8. webpack的require是如何工作的?

    https://stackoverflow.com/questions/35625593/how-does-webpacks-require-work wepback的require函数调用类似于no ...

  9. MySQL案例09:Last_IO_Error: Got fatal error 1236 from master when reading data from binary log

    刚处理完“挖矿”事件,在做最后一个MySQL NBU备份的时候,发现从库有问题,好奇的是怎么主从状态异常没有告警呢?先不管这么多了,处理了这个问题再完善告警内容. 一.错误信息 从库show slav ...

  10. C++发展概述、优缺点及应用领域

    个人观点(C++虽功能强大,但是底层还是调用C,C++为了吸引更多的C程序员,功能过于丰富且复杂,一定程度上反而降低了可编程的实用性.但是不可否认C++ 也是一门艺术.) C++是一门以C为基础发展而 ...