首页 > 其他分享 >HDU - 1114 Piggy-Bank (完全背包)

HDU - 1114 Piggy-Bank (完全背包)

时间:2023-06-08 14:01:32浏览次数:44  
标签:HDU Bank money coins pig amount 1114 bank piggy

Time Limit: 1000MS

 

Memory Limit: 32768KB

 

64bit IO Format: %I64d & %I64u

HDU - 1114


Piggy-Bank



Submit Status




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






310 11021 130 5010 11021 150 301 6210 320 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.





 




Source



Central Europe 1999



//题意:先输入n,m,表示空罐的质量,和现在装了钱的罐子的质量,再输入一个t,表示,有t种规格不同的硬币,接下来输入t行,每行两个数p[i],w[i],分别表示硬币的价值和它的重量,现在问这个罐子里能存放的最少的钱数是多少?



//思路:



就是一个多重背包题,没啥可讲的。




#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define N 50010
using namespace std;
int p[N],w[N];
int dp[N];
int main()
{
	int T,t,n,m,i,j;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		int sum=m-n;
		for(i=1;i<=sum;i++)
			dp[i]=INF;
		scanf("%d",&t);
		for(i=0;i<t;i++)
			scanf("%d%d",&p[i],&w[i]);
		
		for(i=0;i<t;i++)
		{
			for(j=w[i];j<=sum;j++)
			{
				dp[j]=min(dp[j],dp[j-w[i]]+p[i]);
			}
		}
		if(dp[sum]==INF)
			printf("This is impossible.\n");
		else
			printf("The minimum amount of money in the piggy-bank is %d.\n",dp[sum]);
	}
	return 0;
}






 

标签:HDU,Bank,money,coins,pig,amount,1114,bank,piggy
From: https://blog.51cto.com/u_16079508/6439542

相关文章

  • HDU - 2473 (并查集+设立虚父节点(马甲))
    涉及到并查集的删除操作,比较复杂,可以利用虚设父节点的方法:例如:有n个节点,进行m次操作.首先将0~n-1的节点的父节点设置为i+n,n~2n+1的节点的父节点设置为本身,有m次操作,所以要用到m个虚节点,例如0,1,2,3,4,5的父节点为7,8,9,10,11,把他们插入2的集合,所以他们的根节点都为8,当把2从集合......
  • 三个博弈-巴什博奕、威佐夫博弈、尼姆博弈。acm博弈算法笔记HDU 2149,1850,1527
    博弈论(一)、acm博弈基础算法BashGame,NimGame和WythoffGame(即巴什博奕、尼姆博弈、威佐夫博弈)Bash  Game: 同余理论Nim   Game: 异或理论WythoffGame: 黄金分割(二)、三个博弈。1、巴什博奕。只有一堆n个物品,两个人轮流从这堆物品中取物, 规定每次至少取一个,......
  • 树状数组讲解与例题 杭电HDU1166,HDU1556,HDU2689
    树状数组的总结树状数组很巧妙地解决了数列的求和与查找,速度很快。树状数组,它改变数列中某一位,或者求某个区间的和,时间复杂度是O(logN);效率大为改善。下面的图片很好的演示了树状数组的存储原理。(图片来自网络)观察图片,会发现:数组c的每一个元素都管辖着一定范围内的数组a元素的和,比如C......
  • HDU 5542 The Battle of Chibi(树状数组+dp)
    TheBattleofChibiTimeLimit:6000/4000MS(Java/Others)    MemoryLimit:65535/65535K(Java/Others)TotalSubmission(s):1749    AcceptedSubmission(s):621ProblemDescriptionCaoCaomadeupabigarmyandwasgoingtoinvadethewholeSou......
  • ICPC2015(沈阳)HDU5521 建图技巧+最短路
    MeetingTimeLimit:12000/6000MS(Java/Others)  MemoryLimit:262144/262144K(Java/Others)TotalSubmission(s):3533  AcceptedSubmission(s):1136ProblemDescriptionBessieandherfriendElsiedecidetohaveameeting.However,afterFarmerJohndecor......
  • HDU2227(非降子序列的个数)
    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2227题意:给定一个长度为n(n<=100000)的整数序列,求其中的非降子序列的个数。分析:如果n的值比较小,那么就是一个纯粹的dp题。设dp[i]表示以a[i]为结尾非降子序列的个数,其状态转移方程为:      可以看出,这样做的时间复杂度是......
  • HDU4372(第一类斯特林数)
    题目:CounttheBuildings题意:N座高楼,高度均不同且为1~N中的数,从前向后看能看到F个,从后向前看能看到B个,问有多少种可能的排列数。0<N,F,B<=2000首先我们知道一个结论:n的环排列的个数与n-1个元素的排列的个数相等,因为P(n,n)/n=(n-1)!。可以肯定,无论从最左边还是从最右边看,......
  • HDU4633(Polya计数)
    题目:Who'sAuntZhang#include<iostream>#include<string.h>#include<stdio.h>usingnamespacestd;typedeflonglongLL;constLLMOD=10007;LLquick_mod(LLa,LLb){LLans=1;a%=MOD;while(b){if(b&1......
  • HDU4382(特殊的矩阵连乘)
    题目:HarryPotterandCyberSequenceGenerator题意,有两个容器C1,C2,初始的时候C1中有一个数的值为V,给你K个操作,每次都重复这K个操作N遍,最后问你C2中的数是   多少。N<=10^100。1:循环操作的次数巨大,敏感的想到这是矩阵连乘的题目。2:K个操作可以得出一个矩阵,N个K操作就是这个......
  • HDU1524(博弈--有向无环图SG函数)
    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1524题意:在一个有向无环图上有n个顶点,每一个顶点都只有一个棋子,有两个人,每次根据这个图只能将任意一颗棋子移动一步,如果到某一步玩家不能移动时,那么这个人就输.分析:本题是最典型的有向无环图的博弈,利用dfs把所有顶点的SG值......