[HDU2191] To mourn the victims of the 512 Wenchuan earthquake-cherish the present and be grateful for life (multiple backpacks/simple DP)

To mourn the victims of the 512 Wenchuan earthquake—cherish the present and be grateful for life

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

anxious! Food in the disaster area is still short!
In order to save the lives of the compatriots in the disaster-stricken area, you who care about the compatriots in the disaster-stricken area are going to purchase some food to support the disaster-stricken areas by yourself. Now suppose you have a total of n yuan and there are m varieties of rice in the market, each of which is packaged in bags, and the price varies. , And can only be purchased in whole bags.
Excuse me: How many kilograms of grain can you purchase with limited funds?
Write picture description here
Postscript:
Life is a life process full of variables. Natural disasters, man-made disasters, and illnesses are unpredictable threats in our life course.
The moon is full of cloudy and sunny, and people have misfortunes and blessings. The future is an unknown to us. So, we have to do now should be cherished, being grateful -
grateful to their parents, they give us life, we raise adult;
thank a teacher, they granted us the knowledge to teach us how
to thank friends, they let us feel the world Warmth;
thanks to opponents, they make us keep making progress and hard work.
Similarly, we also want to thank the wealth that pain and hardship bring to us~

Input

The input data first contains a positive integer C, indicating that there are C groups of test cases. The first line of each group of test cases is two integers n and m (1<=n<=100, 1<=m<=100), respectively Indicates the amount of expenditure 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<=200,1<=c<=20 ), indicating the price of each bag, the weight of each bag, and the number of bags of the corresponding type of rice.

Output

For each set of test data, please output the maximum weight of rice that can be purchased. You can assume that the funds not only buy all the rice, and you can use up the funds. The output of each instance occupies one line.

Sample Input

1
8 2
2 100 4
4 100 2

Sample Output

400

Source

2008-06-18 "ACM Program Design" Final Exam-Go Sichuan! Go China!

The problem is a naked problem of multiple backpacks, just a change of weight and cost. We know that the 01 backpack has only one of each item, and the complete backpack has an unlimited number of each item. The multiple backpack is a limited number of each item.

So, just run one side on the idea of ​​a complete backpack .

If you haven’t learned a backpack, please refer to the Nine Lessons on Backpack http://www.cnblogs.com/jbelial/articles/2116074.html

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"

using namespace std;

const int maxn = 1e3+5;

int dp[maxn];
int w[maxn],v[maxn],s[maxn];
int n,m;

int max( int a,int b ){
    return a>b ? a:b;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        memset(dp,0,sizeof(dp));
        scanf("%d%d",&n,&m);
        for( int i=0 ; i<m ; i++ ){
            scanf("%d%d%d",&w[i],&v[i],&s[i]);
        }
        int k=1;
        for(int i = 0; i <  m; i++)  
        {  
            for(int j = 1; j <= s[i]; j++)  
            {  
                for(int k = n; k >= w[i]; k--)  
                {  
                    dp[k] = max(dp[k], dp[k-w[i]] +v[i]);  
                }  
            }  
        } 
        printf("%d\n",dp[n]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/thesprit/article/details/52026725