1750. Games Restriction conditions
Time limit: 1 seconds, Memory limit: 32 MB
Title Description
Zeh is a class 04 student, and he has athletic cells in addition to the high performance points. Once the college held a sports meeting, Zeh found that the project is his invincible, of course, he wants to get all the project No1, but a person's energy is always limited, Zeh is only P (power shorthand, a more than 0 and less than 1000 integer) energy. If energy enough to participate in the game, you can win, on the contrary, energy is not enough to participate in the competition, the probability of loss is very large, zeh of course don't want to because loss and unhappy.
The game has n (greater than 0 and less than or equal to 100) races, each of which is held within a specified day D (d greater than or equal to 0), but one day may be simultaneous with multiple races simultaneously, zeh of course cannot simultaneously. At the end of the game, the Dean will personally award a certain amount of prizes for the championship, with only one certificate in the other rankings. Zeh of course is directed at the bonus, he wants to get the most bonuses, younger sister, you can help Zeh Daniel calculate how much he can get the most money?
Input format
The first line is an integer t, which indicates that the problem has a T use case.
The first line of each use case has two positive integers p N, which represent Zeh's energy and the game's project.
The second line to line n+1 is n items, each line has three positive integers d E M, respectively, indicating that this item is held on D Day, requires E's energy, and the prize of the match. The order of the items is ordered by D.
Output format
For each use case, output the most bonuses he can take.
Sample input
114 50 3 70 2 51 4 22 6 142 8 15
Sample output
23
At the beginning of the time with a one-dimensional dp[] to do, dead and alive, looking for a long time did not find the reason, if there is a great God just see, you can help me to check the wrong.
Later switched to the two-dimensional dp[][] to do, smooth AC.
The topic itself is problematic, the outline says D is a positive integer, but the example given in the first of the D is 0, this we first ignore, consider the topic itself.
The topic itself is a typical packet knapsack problem, first of all, the given items are grouped by day and stored with the appropriate data structure.
Then, according to the practice of 0-1 backpack to choose the number of days, and then in a day to do a repeat comparison of the day's projects, the last day only selected to a project, the principle and 0-1 backpack is consistent.
Core code:
1 for(intI=0; i<=d;i++){2 if(xmp[i].size () = =0)3 Continue;4 for(intj=p;j>=1; j--){5 if(i>0) 6dp[i][j]=dp[i-1][j]; 7 for(intk=0; K<xmp[i].size (); k++){8 if(xmp[i][k]<=j) {9 if(i>0)TenDp[i][j]=max (dp[i][j],dp[i-1][j-xmp[i][k]]+xmm[i][k]); One Else ADp[i][j]=max (dp[i][j],dp[i][j-xmp[i][k]]+xmm[i][k]); - } - } themax=Max (max,dp[i][j]); - } -}
The No. 0 Day of the situation special treatment, but also pay attention to Dp[i][j]=max (dp[i][j],dp[i-1][j-xmp[i][k]]+xmm[i][k]); DP[I][J] is not dp[i-1][j].
Because it needs to be compared with the results from the previous project of the day.
if (i>0) dp[i][j]=dp[i-1
has been initialized, so the first entry into the K cycle dp[i][j] is the same day dp[i-1][j] the case.
Enclose all the code:
#include <iostream>#include<vector>#include<algorithm>#include<memory.h>using namespacestd;intMain () {intt,p,n,d,e,m; intdp[101][1001]; Vector<int> xmp[101]; Vector<int> xmm[101]; CIN>>T; while(t--) {cin>>P>>N; for(intI=0;i<101; i++) {xmp[i].clear (); Xmm[i].clear (); } for(intI=0; i<n;i++) {cin>>D>>E>>M; Xmp[d].push_back (E); Xmm[d].push_back (M); } memset (DP,0,sizeof(DP)); intmax=0; for(intI=0; i<=d;i++){ if(xmp[i].size () = =0) Continue; for(intj=p;j>=1; j--){ if(i>0) Dp[i][j]=dp[i-1][j]; for(intk=0; K<xmp[i].size (); k++){ if(xmp[i][k]<=j) { if(i>0) Dp[i][j]=max (dp[i][j],dp[i-1][j-xmp[i][k]]+Xmm[i][k]); ElseDp[i][j]=max (dp[i][j],dp[i][j-xmp[i][k]]+Xmm[i][k]); }} Max=Max (max,dp[i][j]); }} cout<<Max<<Endl; } return 0;}
Group Backpack--sicily 1750