UESTC Summer Training #2 Div.2 A dp, recursive, multi-stage problem

Source: Internet
Author: User

A-ATime limit:336MS Memory Limit:1572864KB 64bit IO Format:%lld &%llu SubmitStatusPracticeSpoj amr11a

Description

Thanks a lot for helping Harry Potter in finding the sorcerer ' s Stone of Immortality in October. Did we don't tell the it is just an online game? uhhh! Now here's the real onsite task for Harry. You is given a MaGrid S (a magic grid) having R rows and C columns. Each cell in this magrid have either a Hungarian Horntail dragon that we intrepid hero have to defeat, or a flask of magic Potion that he teacher Snape have left for him. A dragon at a cell (i,j) takes away | s[i][j]| Strength points from him, and a potion at a cell (i,j) increases Harry's strength by s[i][j]. If He strength drops to 0 or less at any point during his journey, Harry dies, and no magical stone can revive him.

Harry starts from the Top-left corner cell (s) and the Sorcerer's Stone is in the Bottom-right corner cell (r,c). From a cell (i,j), Harry can only move either one cell down or right i.e., to cell (I+1,J) or cell (i,j+1) and he can not Move outside the MaGrid. Harry has used magic before starting he journey to determine which cells contains what, but lacks the basic simple mathema Tical skill to determine what minimum strength he needs to start with to collect the sorcerer ' s Stone. Please help him once again.

Thanks a lot for helping Harry Potter in finding the sorcerer ' s Stone of Immortality in October. Did we don't tell the it is just an online game? uhhh! Now here's the real onsite task for Harry. You is given a MaGrid S (a magic grid) having R rows and C columns. Each cell in this magrid have either a Hungarian Horntail dragon that we intrepid hero have to defeat, or a flask of magic Potion that he teacher Snape have left for him. A dragon at a cell (i,j) takes away | s[i][j]| Strength points from him, and a potion at a cell (i,j) increases Harry's strength by s[i][j]. If He strength drops to 0 or less at any point during his journey, Harry dies, and no magical stone can revive him.

Harry starts from the Top-left corner cell (s) and the Sorcerer's Stone is in the Bottom-right corner cell (r,c). From a cell (I,J),

Harry can only move either one cell down or right i.e., to cell (I+1,J) or cell (i,j+1) and he can not move outside the MA Grid. Harry has used magic before starting he journey to determine which cells contains what, but lacks the basic simple mathema Tical Skill to

Determine what minimum strength he needs to start with to collect the sorcerer ' s Stone. Please help him once again.

Input (STDIN):

The first line contains the number of test cases T. T cases follow. Each test case consists of R C in the first line followed by the description of the grid

In R lines, each containing C integers. Rows is numbered 1 to R from the top to bottom and columns is numbered 1 to C from left to right. Cells with

S[I][J] < 0 contain dragons, others contain magic potions.

Output (STDOUT):

Output T lines, one for each case containing the minimum strength Harry should start with from the cell (all) to a PO Sitive Strength Through

Out he journey to the cell (r,c).

Constraints:

1≤t≤5

2≤r, c≤500

-10^3≤s[i][j]≤10^3

S[1][1] = S[r][c] = 0

Sample Input:

3
2 3
0 1-3
1-2 0
2 2
0 1
2 0
3 4
0-2-3 1
-1 4 0-2
1-2-3 0

Sample Output:

2
1
2

Explanation:

Case 1:if Harry starts with strength = 1 at cell (), he cannot maintain a positive strength in any possible path. He needs at least strength = 2 initially.

Case 2:note this to start from (a) he needs at least strength = 1.

Hint

Added by: Varun Jalan
Date: 2011-12-15
Time limit: 0.336s
Source limit: 50000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All
Resource: Anil KISHORE-ICPC Asia Regionals, Amritapuri 2011

Source

Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121703#problem/A


My Solution

At the beginning of the training is the memory search, but no matter how optimization or tle 3, no way, think of how to write a recursive

But the conversion equation is a little bit of a problem, WA5.

And then I just wanted to understand.

As long as dp[i][j] = max (Dp[i+1][j], dp[i][j+1]) + s[i][j];
if (Dp[i][j] > 0) dp[i][j] = 0;

Here do not discuss s[i][j] the positive and negative, are directly add s[i][j] just fine

And then we can handle the border.


The DP check should focus on the transfer equation. @@facesymbol@@‖∣


#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int maxn = + 8;int s [MAXN] [MAXN], R, C, Dp[maxn][maxn];//int PQ; //!!!!!!    In these minimum values, find the largest one/*void dfs (int a, int b, int sum, int maxmin) {if (Maxmin < PQ) return;    if (a = = R-1 && b = = c-1) {PQ = max (Maxmin, PQ); return;    if (A + 1 < R) Dfs (a + 1, b, sum + s[a+1][b], maxmin = min (sum + s[a+1][b], maxmin)); if (b + 1 < c) Dfs (a, b+1, Sum + s[a][b+1], maxmin = min (sum + s[a][b+1], maxmin));}    */int Main () {#ifdef LOCAL freopen ("A.txt", "R", stdin);    Freopen ("B.txt", "w", stdout);    #endif//LOCAL int T;    scanf ("%d", &t);        while (t--) {//memset (DP, 0x3f3f, sizeof DP);        scanf ("%d%d", &r, &c);            for (int i = 0; i < R; i++) {for (int j = 0; J < C; j + +) {scanf ("%d", &s[i][j]);        }}/*tle 5 pq = 10000000;int SUMT = 0; for (int i = 0; i < C; i+ +) {sumt + = S[0][i];        PQ = min (PQ, SUMT);            } for (int j = 1; j < R; J + +) {sumt + = s[j][c-1];        PQ = min (PQ, SUMT);        } dfs (0, 0, s[0][0], 10000000);            */for (int i = r-1; I >= 0; i--) {if (i = = r-1) dp[r-1][c-1] = 0;                else {dp[i][c-1] = S[i][c-1] + dp[i+1][c-1];            if (Dp[i][c-1] > 0) dp[i][c-1] = 0; } for (int j = c-2; J >= 0; j--) {if (i! = r-1) {Dp[i][j] = max (dp[i+1]                    [j], Dp[i][j+1]) + s[i][j];                if (Dp[i][j] > 0) dp[i][j] = 0;                    } else {dp[i][j] = S[i][j] + dp[i][j+1];                if (Dp[i][j] > 0) dp[i][j] = 0;                }}}/* for (int i = 0, i < R; i++) {for (int j = 0; J < C; j + +) {            printf ("%d", dp[i][j]); } printf ("\n ");        } */if (Dp[0][0] > 0) printf ("1\n");    else {printf ("%d\n",-dp[0][0]+1);} } return 0;}


Thank you!

------ from Prolights

UESTC Summer Training #2 Div.2 A dp, recursive, multi-stage problem

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.