hdu 3496 Watch The Movie(二维费用背包)

二维费用背包问题

有两个限制条件:电影时长和看电影的部数。需要注意一下初始化dp[i][0] = 0,其余未知,因为看电影是一部一部来,所以当前dp值只能衔接前一部的dp值。

View Code
 1 /*
 2 Author:Zhaofa Fang
 3 Lang:C++
 4 */
 5 #include <cstdio>
 6 #include <cstdlib>
 7 #include <sstream>
 8 #include <iostream>
 9 #include <cmath>
10 #include <cstring>
11 #include <algorithm>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 #include <queue>
16 #include <stack>
17 #include <map>
18 #include <set>
19 using namespace std;
20 
21 typedef long long ll;
22 #define DEBUG(x) cout<< #x << ':' << x << endl
23 #define REP(i,n) for(int i=0;i < (n);i++)
24 #define FOR(i,s,t) for(int i = (s);i <= (t);i++)
25 #define FORD(i,s,t) for(int i = (s);i >= (t);i--)
26 #define PII pair<int,int>
27 #define PB push_back
28 #define MP make_pair
29 #define ft first
30 #define sd second
31 #define lowbit(x) (x&(-x))
32 #define INF (1<<30)
33 
34 int dp[1005][105],ti[105],v[105];
35 int main()
36 {
37     //freopen("in","r",stdin);
38     //freopen("out","w",stdout);
39     int T;
40     scanf("%d",&T);
41     while(T--)
42     {
43         int n,m,L;
44         scanf("%d%d%d",&n,&m,&L);
45         REP(i,n)scanf("%d%d",&ti[i],&v[i]);
46         memset(dp,-1,sizeof(dp));
47         REP(i,L+1)dp[i][0] = 0;
48         REP(i,n)
49         {
50             FORD(j,L,ti[i])
51             {
52                 FORD(k,m,1)
53                 {
54                     if(dp[j-ti[i]][k-1]!=-1)dp[j][k] = max(dp[j][k],dp[j-ti[i]][k-1]+v[i]);
55                 }
56             }
57         }
58         if(dp[L][m]!=-1)printf("%d\n",dp[L][m]);
59         else puts("0");
60     }
61     return 0;
62 }

 

posted @ 2013-02-18 20:40  發_  阅读(154)  评论(0编辑  收藏  举报