codevs1169, 51nod1084 (multi-threaded DP)

Source: Internet
Author: User

Let's just say codevs1169,

Title Link: http://codevs.cn/problem/1169/

Test instructions: Chinese question eh ~

Idea: Multi-threaded DP

Use Dp[i][j][k][l] to store a person in (I, j), a person at (K, L) position the maximum contribution to the answer, then the dynamic transfer equation is:

DP[I][J][K][L] = max (max (Dp[i-1][j][k-1][l], dp[i-1][j][k][l-1]), Max (Dp[i][j-1][k-1][l], dp[i][j-1][k][l- 1])) + A[i][j] + a[k][l]

Note: if (i, j) and (K, L) the same words can only a[i][j], a[k][l] only calculate one

Code:

1#include <iostream>2 using namespacestd;3 4 Const intMAXN =Wuyi;5 intA[MAXN][MAXN], DP[MAXN][MAXN][MAXN][MAXN];6 7 intMainvoid){8     intN, M;9CIN >> N >>m;Ten      for(inti =1; I <= N; i++){ One          for(intj =1; J <= M; J + +){ ACIN >>A[i][j]; -         } -     }    the      for(inti =1; I <= N; i++){ -          for(intj =1; J <= M; J + +){ -              for(intK =1; K <= N; k++){ -                  for(intL =1; L <= m; l++){ +DP[I][J][K][L] = max (max (Dp[i-1][j][k-1][L], Dp[i-1][j][k][l-1]), Max (Dp[i][j-1][k-1][L], Dp[i][j-1][k][l-1])); -                     if(i! = k | | J! = L) dp[i][j][k][l] + =A[k][l]; +Dp[i][j][k][l] + =A[i][j]; A                 } at             } -         } -     } -cout << Dp[n][m][n][m] <<Endl; -     return 0; -}
View Code

51nod10084

Title Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1084

This problem is similar to the one above, but the data range is larger, with four-dimensional DP words, regardless of time complexity or space complexity are not allowed

It can be found that if you record the number of steps, you can count the number of steps and the number of rows, then it is similar to the above

Use dp[i][x1][x2] to record step I when the first person walked to line X1, the second person to go to the X2 when the answer to the greatest contribution

Then the dynamic transfer equation is:

DP[I][X1][X2] = max (max (dp[i-1][x1][x2], dp[i-1][x1][x2-1]), Max (dp[i-1][x1-1][x2], dp[i-1][x1-1][x2-1])) + a[x1][y1] + a[x2][y2]

where y1 = i-x1, y2 = i-x2

Note: (x1, y1) and (x2, y2) while A[x1][y1], A[x2][y2] can only calculate one

There is also a pit is the input n, M in the order of M, N//Pit me half a day

Code:

1#include <iostream>2 using namespacestd;3 4 Const intMAXN = 2e2 +Ten;5 intA[MAXN][MAXN], DP[MAXN <<1][MAXN][MAXN];6 7 intMainvoid){8     intN, M;9CIN >> M >>N;Ten      for(inti =1; I <= N; i++){ One          for(intj =1; J <= M; J + +){ ACIN >>A[i][j]; -         } -     } the      for(inti =1; I <= n + m; i++){ -          for(intX1 =1; X1 <= min (n, i); x1++){ -             intY1 = i-X1; -              for(intx2 =1; X2 <= min (n, i); x2++){ +                 inty2 = i-x2; -DP[I][X1][X2] = max (max (Dp[i-1][X1][X2], Dp[i-1][X1][X2-1]), Max (Dp[i-1][X1-1][X2], Dp[i-1][X1-1][X2-1])); +                 if(x1! = x2 | | y1! = y2) dp[i][x1][x2] + =A[x2][y2]; ADP[I][X1][X2] + =A[x1][y1]; at             } -         } -     } -cout << dp[n + m][n][n] <<Endl; -     return 0; -}
View Code

codevs1169, 51nod1084 (multi-threaded 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.