HDU 5234 Happy Birthday Backpack DP

Source: Internet
Author: User

Happy Birthday

Time limit:20 Sec Memory limit:256 MB

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=5234

Description today is Gorwin's birthday. So her mother is going to fulfill one of her wishes. Gorwin said she wanted to eat a lot of cakes. So his mother took her to the cake garden. The garden was divided into n*m squares. In every lattice, there is a cake. Row i, column J of the lattice has a weight of< Span id= "mathjax-span-5" class= "Msubsup" >wi J Kilograms of cake, gorwin from the upper left corner of the lattice to walk, go to the lower right corner (n,m) lattice. In each step, Gorwin can go right or down, that is: Gorwin (i,j), she can walk to (i+1,j) or (i,j+1) (but she cannot walk out of the garden). When Gorwin arrives at a lattice, she can eat or not eat the cake in that lattice. But she can't just eat part of it. Her stomach is not so big, so she can only eat a K-kilogram cake. Now, Gorwin is standing in the upper left corner, looking at the map of the cake garden and trying to find a way to make the most of the cake she eats. Please come and help me.Input

Multiple sets of test data (approximately 15 groups), each set of data in a row given n, M, K.
In the next n rows, line i has M integer wi1,wi2,wi3,?wim, which represents the weight of the M-Cake on line I.
Please process to the end of the file.

[Parameter conventions]
All inputs are integers.
1<=n,m,k<=100
1<= Wij <=100

Output

For each data, output a value that represents the maximum cake weight that gorwin can eat.

Sample INPUT1 1 2 3 2 3 1 2 3 4 5 6Sample Output
0 16
HINT

In the first set of data, Gorwin can't eat part of the cake, so she can't eat any cakes. In the second data, Gorwin follows the path (2,1), (2,2), (2,3) when she passes through a lattice, it eats up the cake in that lattice. So the total weight she eats is 1+4+5+6=16.

Test instructions

Exercises

Backpack Dp,dp[i][j][k] Represents the maximum value of k in the I,J position

And then just transfer the transfer!

Code:

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200001#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f;/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;}    while (x>0) ch[++num]=x%10,x/=10;    while (Num) Putchar (ch[num--]+48); Puts ("");}*/inline ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;}  while(x>0) ch[++num]=x%Ten, x/=Ten;  while(Num) Putchar (ch[num--]+ -); Puts ("");}//**************************************************************************************intg[ the][ the];intdp[ the][ the][ the];intMain () {//freopen ("Test.txt", "R", stdin);    intn,m,k;  while(SCANF ("%d%d%d", &n,&m,&k)! =EOF) {memset (DP,0,sizeof(DP)); Memset (g,0,sizeof(g));  for(intI=1; i<=n;i++)             for(intj=1; j<=m;j++) G[i][j]=read (); intans=0;  for(intI=1; i<=n;i++)        {             for(intj=1; j<=m;j++)            {                 for(intt=g[i][j];t<=k;t++) {Dp[i][j][t]=max (Dp[i][j][t],max (dp[i-1][j][t],dp[i-1][j][t-g[i][j]]+g[i][j])); Dp[i][j][t]=max (Dp[i][j][t],max (dp[i][j-1][t],dp[i][j-1][t-g[i][j]]+g[i][j])); Ans=Max (ans,dp[i][j][t]); }                 for(intt=0; t<=k;t++) {Dp[i][j][t]=max (Dp[i][j][t],max (dp[i-1][j][t],dp[i][j-1][t])); Ans=Max (ans,dp[i][j][t]); }}} printf ("%d\n", ans); }}

HDU 5234 Happy Birthday Backpack DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.