Reprint please specify source [ametake All rights reserved]http://blog.csdn.net/ametake Welcome to see
It's a classic old question. The source is 08 Noip raising group
On the topic
Title Description
Description
Obuchi and Xiao Xuan are good friends and classmates, they always have to talk about endless topics. A quality expansion activities, the class arranged to make a M-row N-column matrix, and Obuchi and small Xuan is arranged at both ends of the matrix diagonal, so they can not directly talk. Fortunately, they can communicate by passing a note. The note to pass through many students to the other hand, Obuchi sitting in the upper left corner of the matrix, coordinates (at a), the small Xuan sits in the bottom right corner of the matrix, coordinates (M,N). From the Obuchi to the small Xuan note can only be passed down or to the right, from the small Xuan to Obuchi note can only be passed up or to the left.
In the activity, Obuchi hope to send a note to the small Xuan, at the same time hope that the small Xuan to reply to him. Every classmate in the class can help them pass, but will only help them once, that is, if the person in the Obuchi hand to the small Xuan Note when help, then in the small Xuan handed to Obuchi when will not help. Vice versa.
There is one more thing to pay attention to, the class each classmate is willing to help a high degree of good and low (note: Obuchi and small Xuan's kindness degree is not defined, input with 0), you can use a 0-100 natural number to express, the greater the number of the more kindness. Obuchi and small Xuan hope as far as possible to find good-hearted students to help pass the note, that is, to find two ways to pass the path, so that the two path to the kindness of the students and the largest. Now, please help Obuchi and Xiao Xuan to find such two paths.
Enter a description input
Description
The first line of input has 2 integer m and n separated by spaces, indicating that there are m rows n columns (1<=m,n<=50) in the class.
The next M-line is a m*n matrix, and the integer in row J of the matrix indicates the kindness of the student sitting in row J of line I. Each row is separated by a space between n integers.
outputs description output
Description
Outputs a total line that contains an integer that represents the maximum value of the sum of the kindness of the student who is involved in passing the note back and forth on both paths.
sample input to
sample
3 3
0 3 9
2 8 5
5 7 0
Sample output Sample
outputs
34
data
size & Hint
30% data satisfies: 1<=m,n<=10
100% data satisfies: 1<=m,n<=50
This problem originally I used to run two times, but not only the space consumption is large and the time is not ideal also will be wrong
So actually the positive solution is multi-threaded DP,
The four-dimensional array indicates that the coordinates of the two strips are the same as the squares taken before.
This problem can also be used with network flow = = Expense Flow
Directly on the code
A long time ago, I was the code of the P-party era.
program P1006; var m,n,i,j,k,l:longint; A:ARRAY[0..50,0..50] of Longint; F:ARRAY[0..50,0..50,0..50,0..50] of Longint; function Max (a,b:longint): Longint; Begin If A>b then exit (a) Else exit (b); End; Begin Readln (m,n); For I:=1 to M does for j:=1 to n do read (a[i,j]); Fillchar (F,sizeof (f), 0); For I:=1 to M does for J:=1 to N does for K:=1 to M does for l:=1 to n does begin if (i=k) and (j=l) then Continue; F[i,j,k,l]:=a[i,j]+a[k,l]+max (Max (F[i-1,j,k-1,l],f[i-1,j,k,l-1]), Max (F[i,j-1,k-1,l],f[i,j-1,k,l-1])); End; Writeln (Max (f[m,n-1,m-1,n],f[m-1,n,m,n-1]+a[m,n])); End.
CSDN does not support pascal= =
Next comes the C + + code
So that's it, go to the backpack = =
--The May fishing Lang recalled No. Small Jyi canoe, dream into Lotus Pu.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Daily Learning" "Checkerboard DP" "Multi-threaded DP" codevs1169 pass the paper puzzle