Title Requirements:
Input: Three integer n,m,l, representing the movie book you want to see, the number of movies sold in the store, the longest time to see, and then give the respective duration and value of n movies.
Output: The maximum value that can be seen if there is no output that satisfies the condition 0
Problem Solving Ideas:
Two-dimensional knapsack problem, a limit is exceeds sum and has the maximum value, the other is the number of movies required value, in order to facilitate the use of two-dimensional backpack to solve the idea, the second condition can be considered as the maximum number of m to solve, and finally find the number is exactly the value of M.
The state transition equation is:dp[i][j]=max{dp[i][j],dp[i-1][j-t[k]]+v[k]}
The code is as follows:
# include <iostream># include <algorithm>using namespace Std;int t[105],v[105];int dp[105][1005];int main () {freopen ("Input.txt", "R", stdin), int t;scanf ("%d", &t), while (t--) {int n,m,l;scanf ("%d%d%d",&n,&m,& l); int i,j,k;for (i=0;i<n;i++) scanf ("%d%d", &t[i],&v[i]); Memset (Dp,-9999999,sizeof (DP)); for (i=0;i<= l;i++) dp[0][i]=0;for (i=0;i<n;i++) for (j=m;j>0;j--) for (k=l;k>=t[i];k--) {if (dp[j][k]<dp[j-1][k-t[i]]+ V[i]) dp[j][k]=dp[j-1][k-t[i]]+v[i];} if (dp[m][l]<0) dp[m][l]=0;printf ("%d\n", Dp[m][l]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu3496watch the Movie (two-dimensional knapsack problem)