HDU 1114 Full Backpack

Source: Internet
Author: User

Piggy-bank

Typical full knapsack problem, see if it happens to be full of savings, if not full DP[F] or INF, the transfer equation is:

Dp[i] = min (Dp[i], Dp[i-w[j]] + p[j]); Problem Description before ACM can do anything, a budget must is prepared and the necessary financial. The main income for this action comes from irreversibly Bound (IBM). The idea behind are simple. Whenever some ACM member has any small, he takes the coins and throws to them into a. You know the "this" process is irreversible, the coins cannot to be removed without the breaking. After a sufficiently long, there should is enough cash in the Piggy-bank to pay everything.

But There is a big problem with piggy-banks. It is isn't possible to determine how much the is inside. So we might break the pig into pieces only to find out this there is isn't enough money. Clearly, we want to avoid this unpleasant situation. The only possibility are to weigh the piggy-bank and try to guess how many coins the are. Assume that we are able to determine the weight of the pig exactly and we know the weights of Rrency. Then There is some minimum amount of, the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash the inside. We need your help. No more prematurely broken pigs!
InputThe input consists of T test cases. The number of them (T) is given on the "the" input file. Each test case begins with a line containing two integers E and F. They indicate the weight of a empty pig and of the pig filled with coins. Both weights are given in grams. No Pig would weigh more than kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is a integer number n (1 <= n <=) 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.
OutputPrint exactly one line to output for each test case. The line must contain the sentence "The minimum amount to the Piggy-bank is X." where x is the minimum amount of Money that can is 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 4Sample Output
The minimum amount of the Piggy-bank is 60.

The minimum amount of the Piggy-bank is 100.

This is impossible.

#include <iostream> #include <vector> #include <algorithm> #include <map> #include <vector > #include <queue> #include <string> #include <set> #include <cmath> #include <cstdio> include<sstream> #include <cstring> #define INF 1000000000//#pragma warning (disable:4996) using namespace
Std
int p[505];
int w[505];
int a[5005];
int dp[10005];
	int main () {//freopen ("S.txt", "R", stdin);
	int t;
	CIN >> T;
	int e, F;
		while (t--) {cin >> e >> F;
		int n;
		CIN >> N;
		for (int i = 0; i < n; i++) {cin >> p[i] >> w[i];
		for (int i = 0; i < 10004 ++i) {dp[i] = INF;
		} Dp[e] = 0; for (int i = e + 1; I <= F; ++i) {for (int j = 0; J < N; ++j) {if (i-w[j) >= e) dp[i] = min (dp[i)
			, Dp[i-w[j]] + p[j]);
		} if (dp[f] = = INF) cout << "This is impossible." << Endl; else cout << "The minimum amount of money in the pigGy-bank is "<< dp[f] <<". "<< Endl;
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.