Nyoj 61 text transfer (1)

Source: Internet
Author: User
Text transfer (1)Time Limit: 2000 MS | memory limit: 65535 kb difficulty: 5
Description

Xiao yuan and Xiao Xuan are good friends and classmates. They always have endless topics together. During a quality expansion activity, the class arranged a matrix of n columns in m rows, while Xiao yuan and Xiao Xuan were arranged at the two ends of the matrix diagonal. Therefore, they could not directly talk to each other. Fortunately, they can communicate by passing a piece of paper. The paper was transferred to the other Party through many students. xiaoyuan sat in the upper left corner of the matrix, with the coordinates () and xiaoxuan sat in the lower right corner of the matrix, with the coordinates (m, n ). The notes sent from xiaoyuan to xiaoxuan can only be passed down or to the right. The notes sent from xiaoxuan to xiaoyuan can only be passed up or left.

During the activity, xiaoyuan hoped to deliver a piece of paper to xiaoxuan and hoped that xiaoxuan could reply to him. Every student in the class can help them, but it will only help them once. That is to say, if this person helps when he passes xiaoyuan to xiaoxuan, so I won't help you when Xiao Xuan handed it to Xiao yuan. And vice versa.


One more thing to note is that the good feelings of everyone in the class who is willing to help are high and low (note: the kindness of Xiao yuan and Xiao Xuan is not defined, and the input is expressed as 0 ), it can be expressed by a natural number ranging from 0 to. The larger the number, the better the heart. Xiao yuan and Xiao Xuan hope to seek help from students with high level of good intentions, that is, to find the two transfer paths back and forth, so that the sum of good intentions of the two paths is the greatest. Now, please help Xiao yuan and Xiao Xuan find these two paths.

 
Input
Enter N (0 <n <100) in the first line to indicate the number of data groups to be tested.
The first row of each group of test data has two integers m and n separated by spaces, indicating that there are m rows in the class with n columns (2 <= m, n <= 50 ).
The next m row is a matrix of M * n. The integer in column J of row I in the matrix indicates the kindness of the students sitting in column J of row I (not greater than 1000 ). N integers in each row are separated by spaces.
Output
Each group of test data is output in a row, which contains an integer, indicating the maximum value of the students' kindness to the two sides.
Sample Input
13 30 3 92 8 55 7 0
Sample output
34
Source
Noip2008
Uploaded
Hzyqazasdf

Problem solving: the legend... Multi-process DP. Run the Tle code first...

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 int t[51][51],dp[51][51][51][51];12 int main() {13     int ks,i,j,r,c,ii,jj,temp;14     scanf("%d",&ks);15     while(ks--) {16         scanf("%d %d",&r,&c);17         memset(dp,0,sizeof(dp));18         for(i = 1; i <= r; i++) {19             for(j = 1; j <= c; j++) {20                 scanf("%d",t[i]+j);21             }22         }23         for(i = 1; i <= r; i++) {24             for(ii = 1; ii <= r; ii++) {25                 for(j = 1; j <= c; j++) {26                     for(jj = 1; jj <= c; jj++) {27                         temp = INT_MIN;28                         temp = max(temp,dp[i][j-1][ii][jj-1]);29                         temp = max(temp,dp[i-1][j][ii-1][jj]);30                         temp = max(temp,dp[i-1][j][ii][jj-1]);31                         temp = max(temp,dp[i][j-1][ii-1][jj]);32                         dp[i][j][ii][jj] = temp+t[i][j]+t[ii][jj];33                         if(i == ii && j == jj) dp[i][j][ii][jj] = temp+t[i][j];34                     }35                 }36             }37         }38         printf("%d\n",dp[r][c][r][c]);39     }40     return 0;41 }
View code

Okay, AC code, dimensionality reduction. From discussion board

 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int v[52][52],f[102][52][52]; 6 int main() { 7     int i,j,k,t,c,T,m,n; 8     scanf("%d",&T); 9     while(T--) {10         scanf("%d%d",&m,&n);11         memset(v,0,sizeof(v));12         memset(f,0,sizeof(f));13         for(i=1; i<=m; i++)14             for(j=1; j<=n; j++)15                 scanf("%d",&v[i][j]);16         c = m+n-2;17         for(k = 1; k < c; k++) {18             t=k+2 >m?m:k+2;19             for(i=1; i<=t; i++)20                 for(j=i+1; j<=t; j++)21                     f[k][i][j]=max(max(f[k-1][i][j],f[k-1][i][j-1]),max(f[k-1][i-1][j],f[k-1][i-1][j-1]))22                                +v[i][k-i+2]+v[j][k-j+2];23         }24         f[c][m][m]=max(f[c-1][m-1][m],f[c-1][m][m-1]);25         printf("%d\n",f[c][m][m]);26     }27     return 0;28 }
View code
 
  

 



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.