POJ1384--DP, full backpack

Source: Internet
Author: User

POJ 1384 DP, full backpack Piggy-bank
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8404 Accepted: 4082

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

310 11021 130 5010 11021 150 301 6210 320 4

Sample Output

The minimum amount of money in the Piggy-bank are 60.The minimum amount of money in the Piggy-bank are 100.This is Impossibl E.

Test instructions: A1,a2,..., An with a bunch of coins of a given face value, (AI and AJ can have the same face value), combined into a minimum total value of exactly the same mass as W
Idea: DP, set the choice range for the first I coin composition quality is exactly J minimum face value is DP (I,J) =max (DP (I-1,J), DP (I-1,j-w[i]) +p[i]), (J>=w[i])
Boundary: DP (I,J) =DP (I-1,J), DP (i) (0) =0 (total mass 0, total face value is 0), initialize Dp={inf},inf that is, the total face value is infinitely large, that is, there is no suitable matching scheme;
Tip: Observing the boundary can make J direct from W[i]-->w, instead of using a scrolling array from 0-->w; to directly place space into one dimension

C + + code:
//poj1384 DP#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<ctype.h>using namespacestd;Const intmaxn=510;Const intinf=1000100;intT;inte,f;intN;intP[MAXN],W[MAXN];intDp[inf];intMain () {CIN>>T;  while(t--) {cin>>E>>F>>o;  for(intI=1; i<=n;i++) {cin>>p[i]>>W[i]; }         for(intI=0; i<=f-e;i++) dp[i]=INF; dp[0]=0;  for(intI=1; i<=n;i++){             for(intj=w[i];j<=f-e;j++) {Dp[j]=min (dp[j-w[i]]+P[i],dp[j]); }        }        if(Dp[f-e]!=inf) cout<<"The minimum amount of money in the Piggy-bank is"<<dp[F-E]<<"."<<Endl; Elsecout<<"This is impossible."<<Endl; }    return 0;}
dp_ Full Backpack

Java code:

//594msImportJava.util.*;ImportJava.io.*; Public classMain {Static Final intmaxn=510; Static Final intinf=1000100;  Public Static intMinintAintb) {        returnA<b?a:b; }     Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intt=In.nextint ();  while(T--! =0){            intE=In.nextint (); intf=In.nextint (); intn=In.nextint (); intp[]=New int[n+1],w[]=New int[N+1];  for(inti=1;i<=n;i++) {P[i]=In.nextint (); W[i]=In.nextint (); }            intdp[]=New int[F-e+1];  for(inti=0;i<=f-e;i++) dp[i]=INF; dp[0]=0;  for(inti=1;i<=n;i++){                 for(intj=w[i];j<=f-e;j++) {Dp[j]=min (dp[j-w[i]]+P[i],dp[j]); }            }            if(Dp[f-e]!=inf) System.out.println ("The minimum amount of money in the Piggy-bank is" +dp[f-e]+ "."); ElseSystem.out.println ("This is impossible.")); }    }}
Dp--java

POJ1384--DP, full backpack

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.