CH5103 Pass the note "linear DP"

Source: Internet
Author: User

5103 Pass the note 0x50"Dynamic Programming" examplesDescribe

Given a n*m matrix A, there is an integer in each lattice. Now you need to find two paths from the upper-left corner (N,M) to the lower-right corner, each step on the path can only go right or down. The number of squares that pass through the path is taken away. Two paths cannot pass through the same lattice. What is the sum of the maximum of the number obtained? N,m≤50.

Input format

The first line has 2 integers of N and m separated by spaces, indicating that there are n rows m columns (1<=n,m<=50).
The next n rows are a n*m matrix, separated by a space between n integers per line.

Output format

An integer that represents the answer.

Sample input
3 30 3 92 8 55 7 0
Sample output
34
Data scope and conventions
    • 30% of the data meet: 1<=m,n<=10
      100% of the data meet: 1<=m,n<=50
Source

CCF NOIP2008 T3

Test instructions: Each lattice in the n*m has a weight, from (a) to (N,m) two roads, (only down or to the right) to seek the sum of the path. The squares that pass are counted only once.

Ideas:

Take "path Length", which is the number of steps currently traversed, as the "stage" of DP. "The path length is n+m-2" because it can only go down or to the right (N,M)

In each phase, the two paths are expanded one step at a time, and the path length is increased by 1, thus moving to the next stage.

You also need to determine the current end position of the two path. and x1+y1 = x2 + y2 = i + 2

So you can use three-dimensional DP maintenance, there are 4 ways to expand each time. and consider whether the two point coordinates are the same after the expansion.

Target is dp[n+m-2][n][n]

1#include <bits/stdc++.h>2#include <iostream>3#include <cmath>4#include <algorithm>5#include <stdio.h>6#include <cstring>7#include <map>8 9 #defineINF 0x3f3f3f3fTen using namespacestd; OnetypedefLong LongLL; A  - intN, M; - Const intMAXN = -; the intG[MAXN][MAXN]; - intDP[MAXN *2][MAXN][MAXN] = {0}; -  - intMain () + { -scanf"%d%d", &n, &m); +      for(inti =1; I <= N; i++){ A          for(intj =1; J <= M; J + +){ atscanf"%d", &g[i][j]); -         } -     } -  -dp[0][1][1] = g[1][1]; -      for(inti =0; I <= n + M-2; i++){ in          for(intX1 =1; X1 <= min (n, i +1); x1++){ -              for(intx2 =1; X2 <= min (n, i +1); x2++){ to                 intY1 = i +2-X1, y2 = i +2-x2; +                 if(x1 = = x2 && Y1 = =y2) { -Dp[i +1][X1][X2] = max (dp[i +1][X1][X2], dp[i][x1][x2] + g[x1][y1 +1]); theDp[i +1][x1 +1][X2 +1] = max (dp[i +1][x1 +1][X2 +1], dp[i][x1][x2] + g[x1 +1][y1]); *                 } $                 Else{Panax NotoginsengDp[i +1][X1][X2] = max (dp[i +1][X1][X2], dp[i][x1][x2] + g[x1][y1 +1] + G[x2][y2 +1]); -Dp[i +1][x1 +1][X2 +1] = max (dp[i +1][x1 +1][X2 +1], dp[i][x1][x2] + g[x1 +1][Y1] + g[x2 +1][y2]); the                 } +  A                 if(x1 = = x2 +1&& Y1 +1==y2) { theDp[i +1][X1][X2 +1] = max (dp[i +1][X1][X2 +1], dp[i][x1][x2] + g[x1][y1 +1]); +                 } -                 Else{ $Dp[i +1][X1][X2 +1] = max (dp[i +1][X1][X2 +1], dp[i][x1][x2] + g[x1][y1 +1] + g[x2 +1][y2]); $                 } -  -                 if(x1 +1= = x2 && Y1 = = y2 +1){ theDp[i +1][x1 +1][X2] = max (dp[i +1][x1 +1][X2], dp[i][x1][x2] + g[x1 +1][y1]); -                 }Wuyi                 Else{ theDp[i +1][x1 +1][X2] = max (dp[i +1][x1 +1][X2], dp[i][x1][x2] + g[x1 +1][Y1] + g[x2][y2 +1]); -                 } Wu             } -         } About     } $printf"%d\n", Dp[n + M-2][n][n]); -     return 0; -}

CH5103 Pass the note "linear 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.