HDU 2686 matrix (multi-process DP)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2686

Multi-process DP, first heard of yesterday...

The question is to find two paths from (1, 1) to (n, n) so that the weights are the largest and the nodes do not overlap.

Let the two processes run at the same time, with K enumeration steps. If X1 = X2 | Y1 = Y2, skip this step. The state transition equation is obtained:

DP (K, X1, Y1, X2, Y2) = max (dp (K-1, x1-1, Y1, x2-1, Y2), DP (K-1, x1-1, Y1, X2, y2-1), DP (K-1, X1, y1-1, x2-1, Y2), DP (K-1, X1, y1-1, X2, y2-1 ))

+ Data (x1, Y1) + data (X2, Y2 );

Since it can only go right or down, the coordinates must be x + y = K. In this way, the dimension can be reduced to three dimensions, and the equation is:

DP (K, x1, x2) = max (dp (K-1, x1, x2), DP (K-1, x1-1, X2), DP (K-1, X1, x2-1 ), DP (K-1, x1-1, x2-1) + data (x1, k-x1) + data (X2, k-x2 );

Code:

# Include <cstdio>
# Include <cstring>
# Define max (A, B) A> B? A: B
Int data [31] [31], DP [62] [31] [31];
Int max (int A, int B, int C, int d ){
A = max (A, B );
C = max (c, d );
A = max (a, c );
Return;
}
Int main (){
Int N, I, J, K;
While (~ Scanf ("% d", & N )){
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
Scanf ("% d", & Data [I] [J]);
Memset (DP, 0, sizeof (DP ));
For (k = 1; k <2 * N-2; k ++ ){
For (I = 0; I <= K; I ++ ){
For (j = 0; j <= K; j ++ ){
If (I = j | I> = n | j> = N) continue;
DP [k] [I] [J] = max (DP [k-1] [I] [J], DP [k-1] [I-1] [J], DP [k-1] [I] [J-1], DP [k-1] [I-1] [J-1]);
DP [k] [I] [J] + = data [I] [k-I] + data [J] [k-J];
}
}
}
DP [k] [n-1] [n-1] = max (DP [k-1] [N-2] [n-1], DP [k-1] [n-1] [N-2]) + data [n-1] [n-1] + data [0] [0];
Printf ("% d \ n", DP [k] [n-1] [n-1]);
}
Return 0 ;}

 

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.