HDU - 1114 Piggy-Bank 完全背包(背包恰好装满)
Piggy-Bank
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. 题意:已知空小猪罐质量与装满钱时的质量,给出每种钱的价值与质量(每种可有无限个),求出小猪罐中钱币可能存在的最小价值(至少多少钱)。
思路:完全背包。注意最优解为最小值,f初始化INF,f[0]=0。能够保证最后结果f[V]为恰好装满。 ps总结:最大价值memset(f,0,sizeof(f));f[j]=max(f[j],f[j-v[i]]+w[i]); 最小价值memset(f,0,sizeof(f));if(f[j]==0) f[j]=f[j-v[i]]+w[i];else f[j]=min(f[j],f[j-v[i]]+w[i]); 最大价值恰好装满memset(f,-INF,sizeof(f));f[0]=0;f[j]=max(f[j],f[j-v[i]]+w[i]); 最小价值恰好装满memset(f,INF,sizeof(f));f[0]=0;f[j]=min(f[j],f[j-v[i]]+w[i]);
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f int f[],v[],w[]; int min(int x,int y)
{
return x<y?x:y;
} int main()
{
int t,V,VV,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d %d",&VV,&V,&n);
V-=VV;
for(i=;i<=n;i++){
scanf("%d%d",&w[i],&v[i]);
}
memset(f,INF,sizeof(f));
f[]=;
for(i=;i<=n;i++){
for(j=v[i];j<=V;j++){
f[j]=min(f[j],f[j-v[i]]+w[i]);
}
}
if(f[V]==INF) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n",f[V]);
}
return ;
}
HDU - 1114 Piggy-Bank 完全背包(背包恰好装满)的更多相关文章
- HDU 1114(没有变形的完全背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 Piggy-Bank Time Limit: 2000/1000 MS (Java/Others ...
- 题解报告:NYOJ #311完全背包(恰好装满)
描述: 直接说题意,完全背包定义有N种物品和一个容量为V的背包,每种物品都有无限件可用.第i种物品的体积是c,价值是w.求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大.本题 ...
- ACM_01背包(恰好装满)
背包2 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量刚好为 W ...
- HDU 1114:Piggy-Bank(完全背包)
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- QDUOJ 分辣条-01背包恰好装满情况
分辣条 发布时间: 2016年6月26日 20:36 最后更新: 2016年6月26日 20:37 时间限制: 1000ms 内存限制: 128M 描述 “你喝的酸奶是我买的,辣条也是我买 ...
- HDU-1114 完全背包+恰好装满问题
B - Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
- HDU 1114 Piggy-Bank (dp)
题目链接 Problem Description Before ACM can do anything, a budget must be prepared and the necessary fin ...
- 题解报告:hdu 1114 Piggy-Bank(完全背包恰好装满)
Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...
- HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)
HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...
随机推荐
- win7怎么设置打印机共享
一.设置好家庭组,让客户机加入家庭组 二.对服务机的打印机进行共享设置,如果保存不成功请在计算机服务那里打开防火墙 三.1.开启guest用户,具体操作:我的电脑右击---管理---本地用户和组--开 ...
- nodejs 版本dockerfile 文件制作,和常用命令
Dockerfile 如下 官方的node6.3的版本有点难下载,建议去网易蜂巢 https://c.163.com/hub pull hub.c.163.com/library/node:6.9 ...
- Spring-data-redis:特性与实例(转载)
原文地址:http://www.cnblogs.com/davidwang456/p/4915109.html Spring-data-redis为spring-data模块中对redis的支持部分, ...
- ansible2
一.ansible模块(yum.pip.service.conr.user.group) 你是否知道ansible一共有多少模块呢?可以用以下命令查看: [root@localhost ~]# ans ...
- 利用iOS原生系统进行人脸识别+自定义滤镜(GPUImage)
人脸识别+滤镜效果(基于GPUImage实现的自定义滤镜) 最近碰到一个好玩的需求.说要客户端这边判定一下是否有人脸.在有的基础上.对相片做进一步的美化滤镜处理. 首先是人脸的识别判定; //将图片对 ...
- (C)位字段(bit-field)
位字段(bit-field) 在存储空间很宝贵的情况下,有可能需要将多个对象保存在一个机器字中,一种常用的方法是:使用类似于编译器符号表的单个二进制位标志集合,外部强加的数据格式(如设备接口等寄存器) ...
- 一次react滚动列表的实践---兼容ios安卓
一.背景 近期项目改版,对原有的h5页面进行了重新设计,数据呈现变成了瀑布流.希望新版兼容ios和安卓两端的情况下,无限制的刷新加载数据.大致效果如下: 整个页面分4部分: 顶部导航 步数状态卡片 用 ...
- 打开Vs2010时,卡在加载工具箱内容 不动了
我是直接打开Visual Studio 2010,而不是以打开解决方案的方式打开.然后就在左下角显示“正在从包‘Microsoft.VisualStudio.IDE.ToolboxControlsIn ...
- ActivemMQ之消息服务器平台(发邮件)
消息服务平台 处理公司内部各种消息业务 比如 发送邮件 发送短信 微信推送 接口有两种类型 异步 同步 同步需求: 当调用消息服务平台,需要返回消息服务平台调用第三方平台接口是否成功 异步需求: ...
- 1 准备学习redis
首先,当然是搜索相关资料了 1 Redis 设计与实现 http://redisbook.com/ 2 Redis快速入门 http://www.yiibai.com/redis/redis_quic ...