codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC Quest
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100500/attachments
Description
Noura Boubou is a Syrian volunteer at ACM ACPC (Arab Collegiate Programming Contest) since 2011. She graduated from Tishreen University where she studied Information Technology. She is the head of the photography team responsible for photo-shooting the Arabs joining the world finals. On the first day of the world finals last year, Dr Mohamed Fo’ad, the ACPC Regional Contest Director, asked Noura to play one of the ICPC Quests. ICPC Quests are a set of challenges that might require the contestants to move around the city searching for some monuments, solving puzzles, or getting a high score in a certain game. The rules of the quests states that the contestants will post the answer to the quest to Twitter using these hashtags #ICPC2014 #QuestN where N is the quest number. The contestant will post a photo or a short video (a Vine) of the challenge he accomplished. The scores of the challenges are accumulated and the one with the highest score will get an Android tablet. Noura was so enthusiastic about the Quest number 14. Quest 14 was about a grid of n rows and m columns, and each cell of the grid contains an integer, and whenever a contestant steps on a cell he is going to add its value to his total accumulated sum. The task was to start from the top left cell located at (1,1) and to move only right or down in order to get to cell (n,m), and during this journey the contestant has to get the maximum sum possible. The player who can get the maximum possible score is announced as the winner of this quest. You will be given the description of the grid, please help Noura determining the maximum possible score she can get.
Input
The first line will be the number of test cases T. Each test case starts with 2 integers n, m where n is the number of rows while m is the number of columns. They will be followed by n rows each containing m numbers, and the absolute value of the number in each cell will not exceed 100. 1 <= T <= 100 1 <= n,m <= 1000 -100 <= celli,j <= 100
Output
For each test case print a single line containing: Case_x:_y x is the case number starting from 1. y is is the required answer. Replace underscores with spaces.
Sample Input
2 3 3 1 2 3 -1 -2 -3 1 1 1 2 3 1 1 2 2 1 5
Sample Output
Case 1: 4 Case 2: 9
HINT
题意
你可以往下走,也可以往右走,然后问你从1,1走到n,m,求路过的和最大可以为多少
题解:
简单dp,直接二维走就好了
代码
#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> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) const int maxn=202501; #define mod 1000000007 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************* ll dp[1011][1011]; ll a[1011][1011]; int main() { int t=read(); for(int cas=1;cas<=t;cas++) { int n=read(),m=read(); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { a[i][j]=read(); } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(i==1) dp[i-1][j]=-inf; if(j==1) dp[i][j-1]=-inf; if(i==1&&j==1) dp[i-1][j]=0; dp[i][j]=max(dp[i-1][j],dp[i][j-1])+a[i][j]; } } printf("Case %d: %lld\n",cas,dp[n][m]); } }