HDU 1114:piggy-bank

Source: Internet
Author: User
Tags min split
Piggy-bank Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 20954 Accepted Submission (s): 10659


Problem 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.
Source Central Europe 1999


A complete backpack template problem, but the title is required is the minimum value, then the first to set a large number of DP array, the MAX function is changed to min.

state transition equation: Dp[j]=min (dp[j],dp[j-a[i].weight]+a[i].value);


Complete Backpack Problem:
there are n items and a backpack with a capacity of V, with unlimited pieces available for each item. The cost of item I is c[i] and the value is w[i]. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.  



Basic Ideas:
The problem is very similar to the 01 knapsack problem, the difference is that each item has unlimited pieces. That is, from the point of view of each item, the strategy associated with it has not taken or not taken two kinds, but take 0 pieces, take 1 pieces, take 2 pieces ... And so many kinds. If you still follow the idea of 01 knapsack, make F[i][v] indicate that the first I item fits into a backpack with a capacity of V maximum weight. The state transfer equation can still be written according to the different strategies of each item, like this: F[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k*c[i]<= v}. This is the same as the 01 knapsack problem with O (n*v) states need to solve, but the time to solve each state is not constant, the time to solve the state F[i][v] is O (V/c[i]), the total complexity is more than O (VN).



a simple and efficient optimization:
complete knapsack problem has a very simple and effective optimization, is this: if two items I, J satisfies C[i]<=c[j] and w[i]>=w[j], then the item J removed, do not consider. The correctness of this optimization is obvious: In any case, the value of the small cost of the high price of J to the inexpensive I, get at least not worse solutions. For randomly generated data, this method tends to significantly reduce the number of items, thus speeding up. This, however, does not improve the complexity of the worst case scenario, as it is possible that specially designed data can not be removed from a single item.  



Convert to 01 knapsack problem Solver:
since 01 knapsack problem is the most basic knapsack problem, then we can consider the complete knapsack problem into 01 knapsack problem to solve. The simplest idea is that, considering the maximum selection of v/c [i] for item I, I can convert item I to v/c[i], and then solve the 01 knapsack problem with the same cost and value. This completely does not improve the time complexity of the basic idea, but this gives us the idea of turning the complete knapsack problem into a 01 knapsack problem: To split an item into multiple items.



A more efficient way to convert is:

The item i is split into a number of items with a cost of c[i]*2^k and a value of w[i]*2^k, where K satisfies c[i]*2^k<v. This is the idea of binary, because no matter the optimal strategy selected a few items I, can always be expressed as a number of 2^k pieces of goods and. It is a great improvement to break each item into an O (log (v/c[i)) piece. But we have better algorithms for O (VN). * O (VN) algorithm This algorithm uses a one-dimensional array, pseudo-code:

For I=1..N for
 V=0..V
   



AC Code:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int dp[1000005];
struct PO
{
    int value;
    int weight;
} A[1000005];
int solve (int ma,int n)
{
    fill (dp,dp+ma+105,10000000);
    dp[0]=0;
    for (int i=0, i<n; i++) for
        (int j=a[i].weight; j<=ma; j + +)
            dp[j]=min (Dp[j],dp[j-a[i].weight]+a[i]. value);
    if (dp[ma]==10000000) return-1;
    return dp[ma];
}
int main ()
{
    int N;
    cin>>n;
    while (n--)
    {
        int ko,ma,n;
        cin>>ko>>ma;
        Ma-=ko;
        cin>>n;
        for (int i=0; i<n; i++)
            cin>>a[i].value>>a[i].weight;
        int So=solve (ma,n);
        printf (so!=-1? ") The minimum amount of money in the Piggy-bank are%d.\n ":" This was impossible.\n ", so);
    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.