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): 19574 Accepted Submission (s): 8285
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 Input
18 22 100 44 100 2
Sample Output
400
Authorlcy just learned two ways. The first is a direct multi-pack:
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include < string.h> #include <cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <set>using namespace std;const int INF = 101; struct a{ int cost, val,num;} E[inf];int Dp[inf];int Main () { int t; cin>>t; while (t--) { memset (DP, 0, sizeof (DP)); int N, m;cin>>n>>m; for (int i = 0; i < m; i++) { cin>>e[i].cost>>e[i].val>>e[i].num; } for (int i = 0; i< m, i++) {for (int j = 1; J <= E[i].num; j + +) {for (int v = n; v >= e[i].cost; v--) {
DP[V] = max (Dp[v], Dp[v-e[i].cost] + e[i].val); }}} cout<<dp[n]<<endl; } return 0;}
The second is a multi-pack to 01 backpacks:
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include < string.h> #include <cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <set>using namespace Std;const int INF = 102 * 20; int dp[inf];struct a{int cost, Val;} E[inf];int Main () {int p, h, C; int t; cin>>t; int n, m; int tot; while (t--) {tot = 0; cin>>n>>m; memset (DP, 0, sizeof (DP)); for (int j = 0; J < m; J + +) {cin>>p>>h>>c; for (int i = 0; i < C; i++) {e[tot].cost = P; E[tot++].val = h; }} for (int i = 0; i < tot; i++) {for (int V = n; V >= E[i].cost; v--) {Dp[v] = max (Dp[v], Dp[v-e[i].cost] + e[i].val); }} cout<<dp[n]<<endl; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hangzhou Electric HDU ACM 2191 Mourning 512 Wenchuan earthquake victims--cherish now, Thanksgiving life (multiple backpack)