Hdu1114 piggy-bank

Source: Internet
Author: User

Piggy-bank

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 8329 accepted submission (s): 4199

Problem descriptionbefore 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 shoshould 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 pig!
 

Inputthe 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 varous 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 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

310 11021 130 5010 11021 150 301 6210 320 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.
 

Sourcecentral Europe 1999
 

Recommendeddy

01 question about backpack

There are n items and a backpack with a capacity of v. The cost of the I-th item is C [I], and the value is W [I]. Solving which items are loaded into a backpack can maximize the total value.

Basic Ideas

This is the most basic problem with a backpack. It features that each item has only one item, and you can choose to put it or not.

Define the state with a subproblem: that is, F [I] [v] indicates the maximum value that a backpack with a capacity of V can obtain when the first I item is placed. The state transition equation is:

f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}

This equation is very important. Basically all the equations related to the backpack are derived from it. Therefore, it is necessary to explain in detail: "Put the first I items into a backpack with a capacity of V, if you only consider the I-th item Policy (put or not put), then it can be converted into a problem that only involves the previous I-1 items. If I items are not put, then the problem is converted to "pre-i-1 items into the capacity of V backpack", the value is f [I-1] [v]; if I items are placed, the problem is converted to "the previous I-1 items are placed in the backpack with the remaining capacity V-C [I ", the greatest value that can be obtained at this time is f [I-1] [V-C [I] plus the value W [I] obtained by placing the I item.

Optimize space complexity

The time and space complexity of the above method are O (VN). The time complexity should no longer be optimized, but the space complexity can be optimized to O.

First, consider how to implement the basic idea mentioned above. There must be a main loop I = 1 .. n, two-dimensional array f [I] [0 .. all values of V. Then, if only one array f [0 .. v]. can we ensure that f [I] [v] represents the state we defined after the end of the I-th loop? F [I] [v] is derived from two subproblems: F [I-1] [v] and f [I-1] [V-C [I, can we ensure that f [I] [v] is pushed (that is, when f [v] is pushed in the I Main Loop) can we get the values of F [I-1] [v] and f [I-1] [V-C [I? In fact, this requires that in each main loop we use v = V .. 0 in order. f [v], this ensures that f [v] f [V-C [I] saves the value of State f [I-1] [V-C [I. The pseudocode is as follows:

for i=1..N    for v=V..0        f[v]=max{f[v],f[v-c[i]]+w[i]};

F [v] = max {f [v], F [V-C [I]} is equivalent to our transfer equation.f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]}Because the current F [V-C [I] is equivalent to the original f [I-1] [V-C [I]. If we change the circular order of V from the forward order to the order, then f [I] [v] is deduced by F [I] [V-C [I, it is not consistent with the meaning of this question, but it is another important backpack problem p02 is the most simple solution, so it is necessary to learn to solve the problem of 01 backpack with only one-dimensional array.

In fact, the program that uses a one-dimensional array to solve the 01 backpack will be used multiple times later, so here we abstract a process to process an item in the 01 backpack, direct calls in future Code are not described.

Zeroonepack indicates processing an item in a 01 backpack. The two parameters cost and weight indicate the cost and value of this item respectively.

procedure ZeroOnePack(cost,weight)    for v=V..cost        f[v]=max{f[v],f[v-cost]+weight}

Note that the processing in this process is different from the pseudo code given above. The preceding example program v = V .. 0 is used to show that every state in the program is solved according to the equation, avoiding unnecessary complexity of thinking. Now that the process has been abstracted as a black box, you can add optimization. Items whose cost is cost will not affect the status f [0 .. cost-1], which is obvious.

With this process, you can write the pseudocode for the 01 backpack problem as follows:

for i=1..N    ZeroOnePack(c[i],w[i]);
Initialization details

We can see that there are actually two different ways to solve the problem of a backpack. Some questions require the optimal solution when "just full of backpacks", and some questions do not require that the backpack be full. One difference is that the implementation methods of these two questions are different during initialization.

If the first method is used, it is required to be filled with a backpack. during initialization, F [0] is 0, and f [1 .. v] is set to-∞, which ensures that the final f [N] is an optimal solution just filled with a backpack.

If you do not need to fill your backpack, but only want the price to be as high as possible, you should set all f [0.. V] to 0 during initialization.

Why? It can be understood as follows: The initialized F array is actually the legal status when no item can be put into a backpack. If a backpack is required to be filled, only a backpack with a capacity of 0 may be filled with nothing with a value of 0. There is no legal solution for a backpack with other capacities, for undefined states, their values should all be-∞. If the backpack does not have to be filled, then any capacity of the backpack has a legal solution: "Nothing is loaded." the value of this solution is 0, therefore, the initial status values are all 0.

This tips can be fully applied to other types of backpack problems, so we will not explain the initialization before the status transfer.

Constant Optimization

In the preceding pseudo code, for V = V .. 1 can be used to improve the lower limit of the loop.

Because we only need the final value of F [v], we only need to know f [V-W [N. Similarly, for the J-ID backpack, you only need to know f [V-sum {W [J .. n]}], that is, in the code

for i=1..N    for v=V..0

Can be changed

for i=1..n    bound=max{V-sum{w[i..n]},c[i]}    for v=V..bound

This is useful when V is large.

Summary

01 the backpack problem is the most basic problem. It contains the most basic idea of the design state and equation in the backpack problem. In addition, other types of knapsack problems can also be converted to 01. Therefore, you must carefully understand the methods of the above basic ideas, the significance of the state transition equation, and how to optimize the space complexity.

The content of the above 01 Q & amp; A backpack issue is excerpted from the 9 lecture on backpack issues.

#include<stdio.h>int dp[10002];int main(){    int t;    int a,b,v;    int n;    int val[505],wei[505];    int i,j;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&a,&b);        scanf("%d",&n);        for(i=0;i<n;i++)            scanf("%d%d",&val[i],&wei[i]);        v=b-a;        for(j=0;j<=v;j++)            dp[j]=0xfffffff;        dp[0]=0;        for(i=0;i<n;i++)            for(j=wei[i];j<=v;j++)                dp[j]=dp[j]<(dp[j-wei[i]]+val[i])?dp[j]:(dp[j-wei[i]]+val[i]);        if(dp[v]!=0xfffffff)        printf("The minimum amount of money in the piggy-bank is %d.\n",dp[v]);        else            printf("This is impossible.\n");    }    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.