F-piggy-bank
Time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Submit Status Practice HDU 1114
Description
Before ACM can do anything, a budget must is prepared and the necessary financial support obtained. The main income for this action comes from irreversibly Bound money (IBM). The idea behind are simple. Whenever some ACM member have any small money, he takes all the coins and throws them into a piggy-bank. You know it is irreversible and the coins cannot be removed without breaking the pig. After a sufficiently long time, there should is enough cash in the Piggy-bank to pay everything that needs to be paid.
But there was a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might the "the pig into Pieces" only "find out" that there are not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility was to weigh the piggy-bank, and try to guess what many coins is inside. Assume that we is able to determine the weight of the pig exactly and so we know the weights of all coins of a given CU Rrency. Then there are some minimum amount of money in the Piggy-bank so 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 in the first line of the input file. Each test case is begins with a line containing the integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights is given in grams. No Pig would weigh more than ten kg, that means 1 <= E <= F <= 10000. On the second line of all test case, there is a integer number N (1 <= n <=) that gives the number of various Coins used in the given currency. Following this is exactly N lines, each specifying one coin type. These lines contain, integers each, pand w (1 <= P <= 50000, 1 <= w <=10000). P is the value of the coin in monetary units, and 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 are X." where x is the minimum amount of Money that can is achieved using coins with the given total weight. If the weight cannot was 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.
Ordinary full backpack is, the most valuable, here is the smallest value, just to change Max to Min, and because it is exactly full, so initialize the DP array attention can be
Dp[i]: Indicates the smallest amount of money to arrive at state I
#include <bits/stdc++.h>using namespace Std;template<class t>inline T Read (t&x) {char C; while ((C=getchar ()) <=32) if (c==eof) return 0; BOOL Ok=false; if (c== '-') Ok=true,c=getchar (); for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 '; if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);} Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x; if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');} Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=1E6+1000;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) typedef long Long LL; typedef double DB;TYPEDEF PAir<int,int> P; #define BUG printf ("---\ n"); #define MOD 100007int Dp[maxn];int main () {int n,e,f; int T; Read (T); while (t--) {read__ (e,f,n); int m=f-e; dp[0]=0;//fills the backpack with a volume of 0, the value is 0 fill (dp+1,dp+m+1,inf);//The other is not yet filled, initialize the INF for (int i=0;i<n;i++) { int p,w; Read_ (P,W); for (int j=w;j<=m;j++) dp[j]=min (Dp[j-w]+p,dp[j]);//and full backpack is just the difference between Max and Min} if (Dp[m]!=inf) printf ("The minimum amount of money on the Piggy-bank is%d.\n", dp[m]); else printf ("This is impossible.\n"); } return 0;}
F-piggy-bank HDU 1114 (Full backpack variants + initialization details)