Hdu3496watch the Movie (two-dimensional knapsack problem)

Source: Internet
Author: User

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)

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.