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 ...
随机推荐
- 动态库对外暴露api的方法
1 windows的动态库 在要export的函数声明的前面加上__declspec(dllexport)标识这个函数是从该dll中export出来给其它模块使用的. declspec是declare ...
- dom 显示 与否 的对 ecmascript 变量的 监听
dom 显示 与否 的对 ecmascript 变量的 监听
- 【题解】CF559C C. Gerald and Giant Chess(容斥+格路问题)
[题解]CF559C C. Gerald and Giant Chess(容斥+格路问题) 55336399 Practice: Winlere 559C - 22 GNU C++11 Accepte ...
- Django项目之【学员管理系统】
Django项目[学员管理系统] 项目规划阶段 项目背景 现实生活中,特别是在学校,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求. 因此需一套方便易用的“学员管理系统”,来提高 ...
- 微信小程序配置详解
在之前已经通过微信公众平台的官方网站https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html,注册好小程序并且登录成功后(这里主 ...
- GDB调试core文件(3)
列出一些常见问题: 一,如何使用core文件 使用core文件 在core文件所在目录下键入: gdb -c core 它会启动GNU的调试器,来调试core文件,并且会显示生成此core文件的程序名 ...
- 获取HDC的几种方法
当需要在显示器上(当然包括打印机等设备上)绘图时,或者写文字的时候,需要取得设备的上下文句柄,即HDC,本文以下都称为HDC.那么,有哪些办法取得HDC呢? 1 BeginPain()和EndPain ...
- 排序List集合
这两天写代码过程中遇到一个需求,需要按照某个字段排序List集合,自己实现了一半,发现网上有个更好的版本,就采用了这个,记录下来. 使用这个工具类要注意一个就是 如果你按照age 字段排序,那么age ...
- iOS 如何查看app提交审核是否使用广告标识符(IDFA)
相信很多人被苹果拒绝过 : 您的 App 正在使用广告标识符 (IDFA).您必须先提供关于 IDFA 的使用信息或将其从 App 中移除,然后再上传您的二进制文件. 那么如何查看app里哪里用到ID ...
- VC++动态链接库(DLL)编程深入浅出:Q&A(原创)
Q1:extern “C” 是做什么用的? A1:一种情况是多个文件中,变量声明或者函数声明,需要extern “C”,这种情况在这里不做讨论. 在dll工程中,被extern "C&quo ...