Hdu2191-mourning for the victims of the 512 Wenchuan earthquake-Cherish the present and be grateful for your life (getting started with multiple backpacks)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2191


The Problem description is urgent! There is still a shortage of food in the disaster area!
In order to save the lives of compatriots in the disaster area, you are prepared to purchase food to support them. Now, suppose you have a total of N yuan, and the market has m kinds of rice, each type of rice is a bagged product, and its price varies, and can only be purchased in the whole bag.
Excuse me: How many kilograms of food can you purchase with limited funds?

Postscript:
Life is a life process full of variables. Natural disasters, man-made disasters, and pain are unpredictable threats in our life journey.
The future is unknown to us because the moon is full and the moon is full of hardships and sorrows. So what we need to do is to cherish the present and be grateful for our lives --
Thanks to our parents, they gave us life and raised us adults;
Thanks to the teachers, they taught us knowledge and taught us how to behave.
Thanks to our friends, they make us feel the warmth of the world;
Thanks to our competitors, they have made us enterprising and hard.
We also want to thank the wealth that the pain and hardships have brought us ~

 

 

Input data first contains a positive integer c, indicating that there are C test cases. The first line of each test case is two integers n and M (1 <= n <= 100, 1 <= m <= 100), indicating the amount of funds and the type of rice, followed by m rows of data, each row contains 3 numbers P, H and C (1 <= P <= 20, 1 <= H <=, 1 <= C <= 20 ), the price of each bag, the weight of each bag, and the number of bags of the corresponding rice.

 

Output for each group of test data, please output the maximum weight of the rice that can be purchased. You can assume that the cost is not only for all the rice, but you don't have to finish the cost. The output of each instance occupies one row.

 

Sample input18 22 100 44 100 2

 

Sample output400

This question is to use a certain amount of money to buy rice (money does not have to be used up), and the number of meters is fixed, so this question is a problem of multiple backpacks, the main problem to be dealt with is the number of meters and the amount of money.

When the product of the price and quantity of rice is greater than the amount of money, it indicates that the rice is sufficient. You can use multiple full backpacks to solve the problem. When the price is less than the amount of money, it indicates that the rice is not sufficient, so we can only choose which bag of rice or not to use the 01 backpack.
Bytes --------------------------------------------------------------------------------------------------

Analysis: based on the previous basic 0-1 backpack knowledge, we can know that the rolling array should be in reverse order, while the complete backpack should be in order (Introduction ). the reason is that every kind of item in a backpack has an infinite number of items and cannot be lifted. however, in multiple backpacks, because each item has a certain number of restrictions, we can allow an order of magnitude, splitting multiple backpacks into 0-1 backpacks is the most direct way to convert complexity into simple problems. you can solve the problem in this way. in this sense, multiple backpacks are actually 0-1 backpacks.

However, there are many questions because there are a large number of items in each of the multiple backpacks, if all the items are split into 0-1 backpacks, it will be TLE. at this time, we need to introduce the binary idea. next we will briefly introduce the application of binary ideas here.

We know that any decimal number can be converted into a corresponding and unique binary number. in addition, the digits of the binary number can reflect the maximum decimal number that can be expressed. for the X-bit binary number, we can indicate that the maximum decimal number is 2 ^ X-1 (that is, x 1 ).

Next, let's take a look at the splitting of multiple backpacks. There are bag [I] items for I. the simplest and most efficient splitting method is to split it into bag [I] items I. however, we can use the binary method mentioned above to improve the splitting efficiency: we only need to split the item into binary digits. this is too abstract. The following is an example:

For the decimal number 10, we use binary to represent 1010. we only need four numbers to indicate 10 States. These four numbers are 1, 2, 4, and 3 respectively. (the reason why the last number is 3 rather than 8 is that if 8 is used, the maximum value is 1 + 2 + 4 + 8 = 15, rather than 10)

1 = 1

2 = 2

3 = 1 + 2

4 = 4

5 = 1 + 4

6 = 2 + 4

7 = 1 + 2 + 4

8 = 1 + 4 + 3

9 = 2 + 4 + 3

10 = 1 + 2 + 4 + 3

 

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include <algorithm>using namespace std;int money,type;int price[105],weigh[105],num[105],dp[105];int max(int a,int b){    return a>b?a:b;}void CompletePack(int price,int weigh){    for(int i=price; i<=money; i++)        dp[i]=max(dp[i],dp[i-price]+weigh);}void ZeroOnePack(int price,int weigh){    for(int i=money; i>=price; i--)        dp[i]=max(dp[i],dp[i-price]+weigh);}void MultiplePack(int price,int weigh,int num){    if(price*num>=money)    {        CompletePack(price,weigh);        return ;    }    int k=1;    while(k<num)    {        ZeroOnePack(k*price,k*weigh);        num-=k;        k>>1;    }    ZeroOnePack(num*price,num*weigh);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d %d",&money,&type);        memset(dp,0,sizeof(dp));        for(int i=1; i<=type; i++)        {            scanf("%d %d %d",&price[i],&weigh[i],&num[i]);            MultiplePack(price[i],weigh[i],num[i]);        }        printf("%d\n",dp[money]);    }    return 0;}

 

Hdu2191-mourning for the victims of the 512 Wenchuan earthquake-Cherish the present and be grateful for your life (getting started with multiple backpacks)

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.