现在的位置: 首页 > 综合 > 正文

HDU 1114 Piggy-Bank

2013年10月14日 ⁄ 综合 ⁄ 共 2806字 ⁄ 字号 评论关闭

Problem Description

Before ACM can do anything, abudget must be prepared and the necessary financial support obtained. The mainincome for this action comes from Irreversibly Bound Money (IBM). The ideabehind is simple. Whenever some
ACM member has any small money, he takes allthe coins and throws them into a piggy-bank. You know that this process isirreversible, the coins cannot be removed without breaking the pig. After asufficiently long time, there should be enough cash in the piggy-bank
to payeverything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to determinehow much money is inside. So we might break the pig into pieces only to findout that there is not enough money. Clearly, we want to avoid this unpleasantsituation. The only possibility
is to weigh the piggy-bank and try to guess howmany coins are inside. Assume that we are able to determine the weight of thepig exactly and that we know the weights of all coins of a given currency. Thenthere 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 ofcash inside the piggy-bank. We need your help. No more prematurely broken pigs!

 

 

Input

The input consists of T testcases. 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. Theyindicate the weight of an empty pig and
of the pig filled with coins. Bothweights 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 aninteger number N (1 <= N <= 500) that gives the number of various coinsused in
the given currency. Following this are exactly N lines, each specifyingone 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, Wis it's weight in grams.

 

 

Output

Print exactly one line ofoutput for each test case. The line must contain the sentence "The minimumamount of money in the piggy-bank is X." where X is the minimum amount ofmoney that can be achieved using coins with
the given total weight. If theweight 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 inthe piggy-bank is 60.

The minimum amount of money inthe piggy-bank is 100.

This is impossible.

 

题目简介:空存钱罐重量E和装满后的重量F。现在有N种钱币,价值为P,重量为W。问最少装多少钱。

方法:一道很简单的DP。状态转移方程:f[j] = min{f[j],f[j-a[i].w]+a[i].p}

 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define INF 99999999

struct coin
{
	int p, w;
};
typedef struct coin C;

C a[1000];
int f[100000];

int cmp(const void *a,const void *b)
 {
	 struct coin *c,*d;
	 c = (struct coin *)a;
	 d = (struct coin *)b;
	 return c->w > d->w ?1 :-1;
 };

int main()
{
	int T, E, F, N, i, j, sum, M;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&E,&F);
		M = F - E;
		scanf("%d",&N);
		for(i = 0;i<N;i++)
		{
			scanf("%d%d",&a[i].p,&a[i].w);
		}
		qsort(a, N,sizeof(a[0]), cmp);
		for(i = 1;i<=M;i++)
		{
			f[i] = INF;
		}
		f[0] = 0;
		for(i = 0;i<N;i++)
		{
			for(j = a[i].w;j<=M;j++)
			{
				if(f[j] > f[j-a[i].w] + a[i].p)
				{
					f[j] = f[j-a[i].w] + a[i].p;
				}
			}
		}
		if(f[M] == INF)
		{
			printf("This is impossible.\n");
		}
		else
		{
			printf("The minimum amount of money in the piggy-bank is %d.\n",f[M]);
		}
	}
	return 0;
}

 

抱歉!评论已关闭.