您的位置:首页 > 其它

集训队的LH(简单DP)

2017-08-23 14:51 232 查看
传送门


集训队的LH

TimeLimit: 1000 ms  MemoryLimit: 65535 K

64-bit integer IO format:%I64d

已解决 | 点击收藏

Problem Description
ACM集训队开始集训了~集训队狂拽酷炫吊炸天的LH同学跋山涉水,翻山越岭,要从家到学校来,为了表示忠诚之心,LH要带很多吃的来分给大家(LH同学好像已经到学校,快找他要吃的),每个地方的特产的质量是不同的,LH的家在(1,1)点,由于赶时间,所以他为了不绕路,只能向学校所在地(n,m)处移动,也就是只能向右和下移动, 有的地方的特产LH不喜欢吃所以(i,j)点的特产可取可不取;但是LH自己也会饿所以在某些地方会吃掉一部分,甚至先透支一部分,所以a(i,
j)可以为负(这个值也可取可不取), 为了犒劳我们他尽可能会多带一点,问他最多带多少质量回来

Input
输入n,m (n,m <= 1000) 再输入n行m列值A(i,j) A(i,j)在 - 1000 ~ 1000 之间。而且A(i,j)可以取可以不取.只能往右或者往下走。 求LH能带回来的最大质量和是多少

Output
输出最大质量

SampleInput

3 3
1 2 3
4 5 6
4 8 9


SampleOutput

27


//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
#include <complex>
using namespace std;

#define pi acos(-1)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e3+10;
const int maxx=1e5+10;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}

inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int dp[maxn][maxn],a[maxn][maxn];
int main()
{
int n,m,t;
W(scanf("%d%d",&n,&m)!=EOF)
{
me(dp,0);
FOR(1,n,i)
{
FOR(1,m,j)
{
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1])+max(0,a[i][j]);
}
}
printf("%d\n",dp
[m]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: