HDU 2,191 Multi-weight backpack

Source: Internet
Author: User

Mourning for 512 Wenchuan earthquake--cherish now, Thanksgiving life

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 22553 Accepted Submission (s): 9524


Problem description Urgent! The food in the disaster area is still short!
In order to save the lives of compatriots in the disaster area, you are prepared to purchase some food support disaster areas, now suppose you have a total of funds N, and the market has m rice, each rice is bagged products, its price range, and can only buy the whole bag.
Excuse me: How many kilograms of grain can you purchase with limited funds?

Postscript:
Life is a life process full of variables, natural disasters, man-made disasters, sickness is the unpredictable threat of our life.
The moon has a cloudy and clear circle, people have unforeseen, the future for us is an unknown. So, what we should do is to cherish the present, Thanksgiving life-
Thank the parents, they give us life, raise our adult;
Thank teachers, they give us knowledge, teach us to be a man
Thank the friends, they let us feel the warmth of the world;
Thanks to the opponents, they make us keep making progress and efforts.
Likewise, we would like to thank the pain and hardship brought to our wealth ~

Input data first contains a positive integer c, which indicates that there is a C set of test cases, the first line of each set of test cases is two integers n and m (1<=n<=100, 1<=m<=100), respectively, the amount of money and the type of rice, then the M row of data, Each line contains 3 numbers of P,h and C (1<=p<=20,1<=h<=200,1<=c<=20), each of which represents the price per bag, the weight of each bag, and the number of bags for the corresponding type of rice.

Output for each set of test data, export the maximum weight that can be purchased for rice, and you can assume that you are spending more than all of the rice, and you can afford to spend it. The output for each instance takes up one row.

Sample Input18 22 100 44 100 2

Sample Output400 topic: All-Chinese, do not explain, the standard multi-knapsack problem thinking analysis: As a backpack problem beginner, this is also the first multi-backpack of their own topic, borrow this topic to say about the knapsack problem of understanding it. First, the knapsack problem is essentially a dynamic programming, can be seen as a branch of dynamic planning, presumably because the knapsack problem is more classic, so as a template, its analysis process and the general DP process is not different, also need to determine the stage, state, and state transfer equation, first of all, the simplest 01 backpack, The so-called 01 backpack, is for the goods to say, an item can only choose to put and not put, so called 01 backpack, in the explanation is generally first by two-dimensional array to unfold the explanation, easy to understand, F[i][j] represents the first I items composed of the volume of the bag of J to obtain the maximum value, the state transfer equation is easy to determine According to article I article release vessel is not put. Then explain the use of the scrolling array to optimize the space complexity, for the scrolling array at first I do not understand, and later to understand, I:1-&GT;N representative stage, J:v->c[i] represents the state of this phase, as for the inner loop from v->c[i], This is determined by the particularity of the 01 knapsack, the item can not be repeated, the current stage state can only be introduced by the previous stage state, if the inner loop becomes c[i]->v, so that the current stage state is introduced by the current stage state, that is, an item has been used several times, does not meet the 01 knapsack requirements, After understanding the template is not a problem, as for the full backpack, and 01 of the different points of the backpack is that each item has no number, if the direct conversion to 01 knapsack problem, time complexity is too large, many problems are difficult, if there are 01 backpack about the inner loop of thinking, here it is easy to think of the inner loop of the order of change , it satisfies the requirement that an item can be used multiple times, and the Complexity O (v*n). And the other problems are basically derived from the two knapsack problems (which can be said to be derived from the 01 backpack), such as the multi-backpack, similar to the full backpack, the only difference is that the number of items is not unlimited , but there is a definite number, for each stage, is divided into two categories, if the c[i]*num[i]>v indicates that the amount of this item is sufficient, with a complete backpack to deal with, if not enough, according to 01 backpack to deal with, but if the direct conversion, easy tle, So here need a step optimization, Num[i] division, divided into 1,2,4.......num[i]-2^k+1 these small backpacks, can prove 0-num[i] between the num[i]+1 numbers can be composed of these numbers do not repeat, It turns into a 01 knapsack problem for several backpacks. Code: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace Std;
const int maxn=100+5;
int C[MAXN],W[MAXN],NUM[MAXN];
int DP[MAXN];
int multi_pack (int c[],int w[],int num[],int n,int m)
{
for (int i=1;i<=n;i++)
{
if (c[i]*num[i]>m)//conversion to complete knapsack problem
{
for (int j=c[i];j<=m;j++)
Dp[j]=max (Dp[j],dp[j-c[i]]+w[i]);
}
Else
{
int k=1;
while (K<num[i])
{
for (int j=m;j>=k*c[i];j--)
Dp[j]=max (Dp[j],dp[j-k*c[i]]+k*w[i]);
Num[i]-=k;
k=k<<1;
}
for (int j=m;j>=num[i]*c[i];j--)
Dp[j]=max (Dp[j],dp[j-num[i]*c[i]]+num[i]*w[i]);
}
}
return dp[m];
}
int main ()
{
int t;
cin>>t;
while (t--)
{
Memset (Dp,0,sizeof (DP));
int n,m;
cin>>m>>n;
for (int i=1;i<=n;i++)
scanf ("%d%d%d", &c[i],&w[i],&num[i]);
Cout<<multi_pack (c,w,num,n,m) <<endl;
}
}

HDU 2,191 Multi-weight 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.