杭电 1114 Piggy-Bank【完全背包】

解题思路,首先很容易想到方程f[v]=min(f[v],f[v-w[i]+p[i]),因为是要求当包装满的时候(因为题目中给出的是包的质量是一定的),包里面装的钱最少,所以将f[]初始化成一个很大的数。

然后对于这个循环

for(i=1;i<=n;i++)

{

for(v=w[i];v<=m;v++)

f[v]=min(f[v],f[v-w[i]+p[i]);//可以理解为只要不超过包的容量,你可以任意放入该种硬币。
}

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.

#include<stdio.h>
#define max 1000000000
int p[50005],w[10010],f[10010];
int min(int a,int b)
{
	if(a<b)
		return a;
	else
		return b;
}
int main()
{
	int ncase,i,v,m,e,r,n;
	while(scanf("%d",&ncase)!=EOF)
	{
		while(ncase--)
		{
			for(i=1;i<10010;i++)
				f[i]=max;
			scanf("%d %d",&e,&r);
			m=r-e;
			scanf("%d",&n);
			for(i=1;i<=n;i++)
				scanf("%d %d",&p[i],&w[i]);

			for(i=1;i<=n;i++)
			{
				for(v=w[i];v<=m;v++)
					f[v]=min(f[v],f[v-w[i]]+p[i]);
			}
			if(f[m]<max)
			printf("The minimum amount of money in the piggy-bank is %d.\n",f[m]);
			else
				printf("This is impossible.\n");

		}

	}
}
时间: 2024-10-05 11:32:01

杭电 1114 Piggy-Bank【完全背包】的相关文章

饭卡------HDOJ杭电2546(还是01背包!!!!!!)

Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩余金额为负),否则无法购买(即使金额足够).所以大家都希望尽量使卡上的剩余金额最少. 某天,食堂中有n种菜出售.每种菜可购买一次. 已知每种菜的价格以及卡上的剩余金额,问最少可使卡上的剩余金额为多少. Input 多组数据.对于每组数据: 第一行为正整数n.表示菜的数量.n<=1000. 第二行包含

杭电1114

1 //完全背包,有待改良 2 #include<stdio.h> 3 #include<string.h> 4 typedef struct 5 { 6 int val,room; 7 } cus; 8 int an[10005]; 9 cus inf[505]; 10 int main() 11 { 12 int n; 13 scanf("%d",&n); 14 while(n--) 15 { 16 memset(an,0,sizeof an); 1

杭电 2159 fate(二维背包费用问题)

FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15224    Accepted Submission(s): 7203 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现在的问

Bone Collector(杭电2602)(01背包)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31604    Accepted Submission(s): 13005 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

湫湫系列故事——减肥记I(杭电4508)(完全背包)

湫湫系列故事--减肥记I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2612    Accepted Submission(s): 1345 Problem Description 对于吃货来说,过年最幸福的事就是吃了,没有之一! 但是对于女生来说,卡路里(热量)是天敌啊! 资深美女湫湫深谙"胖来如山倒,胖去如抽丝"的道

Big Event in HDU(杭电1171)(01背包+完全背包)

Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24708    Accepted Submission(s): 8700 Problem Description Nowadays, we all know that Computer College is the biggest department

杭电 1248 寒冰王座(完全背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1248 寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10792    Accepted Submission(s): 5476 Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票

杭电 1203 I NEED A OFFER!(01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1203 I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15759    Accepted Submission(s): 6259 Problem Description Speakless很早就想出国,现在他已经考完了

杭电 2546 饭卡(01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11285    Accepted Submission(s): 3880 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一