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 ...
随机推荐
- animate和scrollTop的使用
// 平滑滚动到ola结果位置 var scrollHeight = $("#wrap_div")[0].scrollHeight; var curDivHeight = $(&q ...
- An Overview of Query Optimization in Relational Systems
An Overview of Query Optimization in Relational Systems
- Grunt学习笔记【8】---- grunt-angular-templates插件详解
本文主要讲如何用Grunt打包AngularJS的模板HTML. 一 说明 AngularJS中使用单独HTML模板文件的地方非常多,例如:自定义指令.ng-include.templateUrl等. ...
- API的理解和使用——有序集合
有序集合常用的命令 命令 功能 zadd key score member [score member ... ] 添加元素 zcard key 计算成员个数 zscore key member 计算 ...
- 改善程序与设计的55个具体做法 day1
博客好久没更新了,就从这本读书笔记开始吧. 条款01: 视C++为一个语言联邦 C++可视为有四个次语言组成的: 1.C语言 2.Object-Oriented C++ (面向对象C++) 3.Tem ...
- elasticsearch 简单聚合查询示例
因为懒癌犯了,查询语句使用的截图而不是文字,导致了发布随笔的时候提示少于150字的随笔不能发布. 我就很郁闷了. 下面的查询都是前段时间工作中使用过的查询语句. 开始的时候是使用nodejs构建es查 ...
- 使用git连接到Github
直奔主题,使用git连接到Github步骤如下: 1. 安装git yum install git 或者 sudo get-apt install git git-core 2. 全局配置 git c ...
- 《CSS权威指南(第三版)》---第四章 值和单位
本章主要讲解的是一些属性声明用的值: CSS中的值主要有数字,百分数,颜色, 1.颜色: rgb(100%,100%,100%) OR rgb(255,255,255) OR #FF0000 WE ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
- 关于RabbitMQ简介
RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有很多公开标准 ...