HDU 5234 DP背包

题意:给一个n*m的矩阵,每个点是一个蛋糕的的重量,然后小明只能向右,向下走,求在不超过K千克的情况下,小明最终能吃得最大重量的蛋糕。

思路:类似背包DP;

状态转移方程:dp[i][j][k]----在i,j位置时,最大容量为k时的最大值;

做背包循环一般从1开始,因为需要坐标-1的情况,从0开始需要特判,而且容易RE;

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <string>
 7 #include <sstream>
 8 #include <algorithm>
 9 #define Max 2147483647
10 #define INF 0x7fffffff
11 #define N 110
12 #define ll long long
13 #define mem(a,b) memset(a,b,sizeof(a))
14 #define repu(i, a, b) for(int i = (a); i < (b); i++)
15 const double PI=-acos(-1.0);
16 using namespace std;
17 int dp[N][N][N];
18 int w[N][N];
19 int main()
20 {
21     int n,m,k;
22     while(~scanf("%d%d%d",&n,&m,&k))
23     {
24         repu(i,0,1+n)
25         repu(j,0,m+1)
26         repu(p,0,k+1)
27         dp[i][j][p] = 0;
28         repu(i,1,1+n)
29         repu(j,1,1+m)
30         scanf("%d",&w[i][j]);
31         repu(i,1,n+1)
32         {
33             repu(j,1,m+1)
34             {
35                 for(int p = w[i][j]; p<=k; p++)
36                 {
37                     int t = max(dp[i-1][j][p],dp[i][j-1][p]);///左边,上边
38                     int s = max(dp[i-1][j][p-w[i][j]]+w[i][j],dp[i][j-1][p-w[i][j]]+w[i][j]);
39                     dp[i][j][p] = max(t,s);
40                 }
41             }
42         }
43         int maxn = 0;
44         repu(p,0,k+1)
45         maxn = max(dp[n][m][p],maxn);
46         printf("%d\n",maxn);
47     }
48     return 0;
49 }

背包

时间: 2024-10-20 22:52:17

HDU 5234 DP背包的相关文章

HDU 4901 DP背包

给你n个数,问你将数分成两个数组,S,T ,T 中所有元素的需要都比S任意一个大,问你S中所有元素进行 XOR 操作和 T 中所有元素进行 &操作值相等的情况有多少种. DP背包思路 dpa[i][j][0]  表示从左开始到i,不取i,状态为j的方案数 dpa[i][j][1]  表示从作开始到i,取i,状态为j的方案数 dpb[i][j]      表示从右开始到i,状态为j的方案数 因为S集合一定在T集合的左边,那么可以枚举集合的分割线,并且枚举出的方案要保证没有重复,如果要保证不重复,只

HDU 5234 Happy birthday --- 三维01背包

HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置(i,j)有一个容量为k的背包所能获得的最大价值 决策:a[i][j]处的数是否选取 不选取: dp[i][j][k]= max(dp[i-1][j][k], dp[i][j-1][k]) 选取:首先要求k >=a[i][j],那么dp[i][j][k] = max(dp[i-1][j][k-w[i

hdu 5389 dp类似背包

http://acm.hdu.edu.cn/showproblem.php?pid=5389 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft. Stilwell is enjoying the first chapter of this

HDU 1561 The more, The Better(树形dp+背包)

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6000    Accepted Submission(s): 3548 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻

HDU 1011 Starship Troopers(树形dp+背包)

Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13109    Accepted Submission(s): 3562 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of

hdu 1171 Big Event in HDU(母函数|多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:有n种物品,给出每种物品的价值和数目,要将这些物品尽可能的分成相等的两份A和B且A>=B ,输出A,B. 母函数可以过,但感觉最直接的方法应该是多重背包. 母函数的话,也是按总价值的一半求,从一半到小枚举,直到找到系数不为0的就是B. #include <stdio.h> #include <iostream> #include <map> #include <

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu1011 树形dp背包

http://acm.hdu.edu.cn/showproblem.php?pid=1011 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with

HDU Flowers 完全背包

Flowers Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2709    Accepted Submission(s): 1811 Problem Description As you know, Gardon trid hard for his love-letter, and now he's spending too much