111qqz的小窝

老年咸鱼冲锋!

hdu 5234 (bc #42 C)Happy birthday(dp)

Happy birthday

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 672    Accepted Submission(s): 302

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.

 

 

Source
 

 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5338 5337 5336 5335 5334 
 
01 背包,二维而已
一遍ac,真是好开心
01背包对于第i个物品的状态是只能由 i-1得到 (所以倒着循环,在计算i-1状态的时候总保证i的状态已经写出来了,可以省一维空间)
而这道题在于,对于(i,j) 的状态,可以由 (i-1,j) 和 (i,j-1)得到,每一个对应取或者不取,一共就是四种状态
即:由上面不取当前得到,由上面取当前得到,由左边不取当前得到,由左边取当前得到。
状态转移方程为:
初始化很容易想到,没取的时候价值是0
最后答案在 dp[n][m][i]  (1=

 

说点什么

您将是第一位评论人!

提醒
wpDiscuz
粤ICP备18103363