原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5234

Happy birthday

Problem Description

Today is Gorwin’s birthday. So her mother want to realize her a wish. Gorwin says that she wants to eat many cakes. Thus, her mother takes her to a cake garden.

The garden is splited into n* m grids. In each grids, there is a cake. The weight of cake in the i-th row j-th column is wij kilos, Gorwin starts from the top-left(1,1) grid of the garden and walk to the bottom-right(n,m) grid. In each step Gorwin can go to right or down, i.e when Gorwin stands in (i,j), then she can go to (i+1,j) or (i,j+1) (However, she can not go out of the garden).

When Gorwin reachs a grid, she can eat up the cake in that grid or just leave it alone. However she can’t eat part of the cake. But Gorwin’s belly is not very large, so she can eat at most K kilos cake. Now, Gorwin has stood in the top-left grid and look at the map of the garden, she want to find a route which can lead her to eat most cake. But the map is so complicated. So she wants you to help her.

Input

Multiple test cases (about 15), every case gives n, m, K in a single line.

In the next n lines, the i-th line contains m integers wi1,wi2,wi3,⋯wim which describes the weight of cakes in the i-th row

Please process to the end of file.

[Technical Specification]

All inputs are integers.

1<=n,m,K<=100

1<=wij<=100

Output

For each case, output an integer in an single line indicates the maximum weight of cake Gorwin can eat.

Sample Input

1 1 2
3
2 3 100
1 2 3
4 5 6

Sample Output

0
16

Hint

In the first case, Gorwin can’t eat part of cake, so she can’t eat any cake.

In the second case, Gorwin walks though below route (1,1)->(2,1)->(2,2)->(2,3). When she passes a grid, she eats up the cake in that grid. Thus the total amount cake she eats is 1+4+5+6=16.

题解

做了那么多dp,发现根据数据范围确定dp姿势是非常科学的。。。

那么,这道题的数据范围是100,多半需要用O(n3)O(n^3)O(n3)dp,我们再考虑一下题目本身:

我们可以将每个格子看做一个背包,背包的权值为格子里的蛋糕重量。我们先考虑这样一个二维的背包,dp[i][j]可以从上面和左边的点转移过来,如过加上当前点的权值大于上限,那么不吃,反之就吃。但这样实际上是一个贪心策略,很可能无法达到最优解。

但根据数据我们推算出的复杂度明明是O(n3)O(n^3)O(n3)啊,所以我们可以再加一维,布尔数组dp[i][j][k]表示在点(i,j)我们可以吃到k千克蛋糕,我们可以这样转移状态:

如果我们吃了这个点的蛋糕,就可以如此转化:

$dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-val[i][j]]);$ $dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-val[i][j]]);$ 表示我们可以从上面和左边没有吃这个点蛋糕的状态$(k-val[i][j])$吃掉这个点的蛋糕从而变成$k$。

如果没有吃这个点的蛋糕,显然有:

$dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k]);$ $dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k]);$ 记得初始化。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=105;
int n,m,p,val[M][M];
bool dp[M][M][M];
void in()
{memset(dp,0,sizeof(dp));for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)scanf("%d",&val[i][j]),dp[i][j][0]=1;
}
void ac()
{for(int i=1;i<=m;++i)dp[0][i][0]=1;for(int i=1;i<=n;++i)dp[i][0][0]=1;for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){for(int k=val[i][j];k<=p;++k){dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-val[i][j]]);dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-val[i][j]]);}for(int k=1;k<=p;++k){dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k]);dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k]);}}for(int i=p;i>=0;--i)if(dp[n][m][i]){printf("%d\n",i);return;}
}
int main()
{while(scanf("%d%d%d",&n,&m,&p)!=EOF)in(),ac();return 0;
}

HDU5234 Happy birthday相关推荐

  1. [DP]hdu5234

    题意: 在一个矩形的矩阵中,从1,1出发,每次只能向右或者下走,每次可以选择吃或者不吃每个格子里的蛋糕,问在容量为C的情况下,最多吃多少? 分析: 01背包的变形. dp[i][j][k]表示在i,j ...

最新文章

  1. Linux系统学习笔记:文件描述符标志
  2. 获取有效工作日封装类(原创)
  3. iis应用程序池监控方法实例
  4. 大批物联网设备扎堆CES 物联网产业化提速
  5. 移动端UL列表无法平滑向下滚动问题
  6. pc计算机怎么设置域名管理,如何设置域名的DNS服务器 -电脑资料
  7. 台大李宏毅Machine Learning 2017Fall学习笔记 (16)Unsupervised Learning:Neighbor Embedding
  8. 数字图像中高频噪声的滤波
  9. Linux之vim常用扩展操作
  10. 网络安全从业者“行话”
  11. 《如何高效学习》读后感
  12. 浙大计算机学院12月12日毅行,感谢参与lt;2020年秋季浙大飘渺毅行gt;
  13. 网页中使用的特殊字体(webdings, wingdings 2)续
  14. (深度学习快速入门)人工智能、机器学习和深度学习总体概述
  15. 倍市得聊体验:《梦华录》里的营销哲学
  16. Python学习案例2——数独解题及出题程序
  17. 以太网 TCP协议交互过程中出现丢包时的解决机制,超时重传、快速重传、SACK与DSACK
  18. SSH 文件操作命令
  19. xml根据属性去重。如csprj去重
  20. l7sa008b故障代码_奥克斯空调故障代码大全

热门文章

  1. Hugging Face Course-Introduction学习小记 (part1)
  2. oracle服务器cpu 100,Oracle数据库服务器CPU一直100%怎么处理-sql优化方面
  3. matlab连接散射点,使用小波散射做信号分类
  4. php局部缓存,Smarty局部缓存的几种方法简介_php实例
  5. 查询mysql当前连接并删除_查看mysql已有用户并删除
  6. Shiro权限控制框架
  7. 谁能告诉我war包的作用及使用方法。。。。。。
  8. 【原创】Mapped Statements collection does not contain value for DaoImpl.method
  9. 基本入门的C/C++算法总结
  10. 数组最大子数组和(续)之动态规划