程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 3496 Watch The Movie

hdu 3496 Watch The Movie

編輯:C++入門知識

Watch The Movie

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5045 Accepted Submission(s): 1595


Problem Description

New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight and will be very busy after tonight. She like watch cartoon very much. So she wants her uncle to buy some movies and watch with her tonight. Her grandfather gave them L minutes to watch the cartoon. After that they have to go to sleep.
DuoDuo list N piece of movies from 1 to N. All of them are her favorite, and she wants her uncle buy for her. She give a value Vi (Vi > 0) of the N piece of movies. The higher value a movie gets shows that DuoDuo likes it more. Each movie has a time Ti to play over. If a movie DuoDuo choice to watch she won’t stop until it goes to end.
But there is a strange problem, the shop just sell M piece of movies (not less or more then), It is difficult for her uncle to make the decision. How to select M piece of movies from N piece of DVDs that DuoDuo want to get the highest value and the time they cost not more then L.
How clever you are! Please help DuoDuo’s uncle.


Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case:
The first line is: N(N <= 100),M(M<=N),L(L <= 1000)
N: the number of DVD that DuoDuo want buy.
M: the number of DVD that the shop can sale.
L: the longest time that her grandfather allowed to watch.
The second line to N+1 line, each line contain two numbers. The first number is the time of the ith DVD, and the second number is the value of ith DVD that DuoDuo rated.


Output

Contain one number. (It is less then 2^31.)
The total value that DuoDuo can get tonight.
If DuoDuo can’t watch all of the movies that her uncle had bought for her, please output 0.


Sample Input

1
3 2 10
11 100
1 2
9 1


Sample Output

3

二維狀態的01背包,且有一維要正好裝滿。

最後輸出時,因為時間為L時,f[m][L]不一定正好裝有物品,故要從所有f[m][ ]中查找滿足條件的最大值。

#include
#include
#include
#define N 105
const int inf=-0x7fffffff;
int f[N][N*10];
int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
    int i,j,k,n,m,l,T;
	int c[N],w[N];
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&l);
		for(i=0;i<=m;i++)
			for(j=0;j<=l;j++)
				f[i][j]=inf;
		f[0][0]=0;         //因為要恰好m個影片,故要有一維正好裝滿
		for(i=0;i0;j--)
			{
				for(k=l;k>=c[i];k--)
				{
					f[j][k]=max(f[j][k],f[j-1][k-c[i]]+w[i]);
				}
			}
		}
		int ans=0;
		for(i=1;i<=l;i++)       //最後要在所有滿足m個影片中找最大值
			ans=max(ans,f[m][i]);
		if(ans>0)
			printf("%d\n",ans);
		else
			printf("0\n");
	}
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved