Hdu3496-watch the Movie

Source: Internet
Author: User

Describe:

New semester is coming, and Duoduo have to go to school tomorrow. She decides to has fun tonight and'll be very busy after tonight. She like watch cartoon very much. So she wants her uncle-buy some movies and watch with her tonight. She grandfather gave them L minutes to watch the cartoon. After that they has to go to sleep.

Duoduo list N piece of movies from 1 to N. All of them is her favorite, and she wants her uncle buy for her. She give a value VI (vi > 0) of the of the N piece of movies. The higher value a movie gets shows that Duoduo likes it more. Each movie have a time Ti to play over. If a movie Duoduo choice to watch she won ' t stop until it goes to end.

But there was a strange problem, the shop just sell M piece of movies (no less or more then), It's difficult for her uncl E to make the decision. How to select M Piece of movies from N piece of DVDs, Duoduo want to get the highest value and the time they cost not More and then L.

How clever are! Please help Duoduo ' s uncle.

The first line of the input file contains a single integer t (1≤t≤10), the number of test cases, followed by input dat A For each test case:
The first line is:n (N <=), M (M<=n), L (L <= 1000)
N:the number of DVDs that Duoduo want buy.
M:the number of DVDs that the shop can sale.
L:the longest time that she grandfather allowed to watch.
The second line-to-n+1 line is contain and numbers.    The first number is the time of the ith DVD, and the second number are the value of the ith DVD that Duoduo rated. contain one number. (It's less then 2^31.)
The total value of that Duoduo can get tonight.
If Duoduo can ' t watch all of the movies, and her uncle had bought for hers, please output 0. Code:

The first backpack is the time for the movie to accumulate, and the second backpack is the number of movies.

It is worth noting that the number of the film backpack requires that the solution must be exactly m, that is, "just full ", according to the backpack nine, if the backpack is full, then only the capacity of 0 of the backpack can be loaded in nothing and value of 0 is "just full", Other capacity of the backpack are not valid solution, belongs to the undefined state, should be assigned to-∞.

Finally, if the value is negative , no solution is indicated.

#include <stdio.h>#include<string.h>#include<iostream>#include<stdlib.h>#include<math.h>using namespacestd;#defineN 105#defineM 1005#defineMAX 999999intMain () {intTC; intn,m,l;//N is the number of items, m is the maximum quantity that can be bought, and L is the cumulative maximum value of the time    intT[n],v[n],dp[m][n];//DP One-dimensional is time, two-dimensional is quantityscanf"%d",&TC);  while(tc--) {scanf ("%d%d%d",&n,&m,&l);  for(intI=1; i<=n;i++) scanf ("%d%d",&t[i],&V[i]);  for(intI=0; i<=l;i++ ){             for(intj=0; j<=m;j++ ){                if(j==0) Dp[i][j]=0; ElseDp[i][j]=-MAX; }        }         for(intI=1; i<=n;i++ ){             for(intj=l;j>=t[i];j--) {//Time                 for(intk=m;k>=1; k--) {//QuantityDp[j][k]=max (dp[j][k],dp[j-t[i]][k-1]+V[i]); }            }        }        if(dp[l][m]<0) dp[l][m]=0;//a negative representation has no solutionprintf"%d\n", Dp[l][m]); } System ("Pause"); return 0;}

Hdu3496-watch the Movie

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.