LightOJ1036 A Refining Company (DP)

Source: Internet
Author: User

The topic probably says there is a n*m grid map, with each lattice having uranium or radium mines. The north of the map is the Radium Mine processing plant, the west of the uranium processing plant, and by laying in the grid from south to North (radium) or from east to West (uranium) track to send minerals to the processing plant. A lattice can only be paved with a track, that is, to transport uranium to transport radium, different orbits can not intersect. Now know the number of each lattice of uranium and radium on the map, and ask how to lay the track so that the number of minerals sent to the processing plant is highest.

    • First clear to transport a certain lattice of radium ore to the north of the processing plant, the track must be straight and continuous upward, and uranium is the same, the horizontal continuous left track.
    • In addition, because the number to obtain the most, each lattice must be paved track, contrary to know.
    • It can then be found that the lattice (i,j) is irrelevant to the case of the lattice (0,0) to the bounding rectangle of the lattice (i-1,j-1), so that the DP is considered:
      1. DP[0][I][J] represents the maximum number of squares (I,J) that can be obtained by a lattice (0,0) into a rectangular lattice (i,j).
      2. DP[1][I][J] represents the maximum number of squares (I,J) that can be obtained from a lattice (0,0) to a rectangle surrounded by a lattice (I,J)

Transfer:

      1. DP[0][I][J] is the amount of uranium from Max (Dp[0][i-1][j],dp[1][i-1][j]) +sum (i,0) ... (I,J) The number of uranium in the lattice)
      2. DP[1][I][J] Similarly
    • And the final result is Max (Dp[0][n-1][m-1],dp[1][n-1][m-1])
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 intu[555][555],r[555][555],d[2][555][555];7 intMain () {8     intt,n,m;9scanf"%d",&t);Ten      for(intCse=1; cse<=t; ++CSE) { Onescanf"%d%d",&n,&m); A          for(intI=0; i<n; ++i) { -              for(intj=0; j<m; ++j) { -scanf"%d",&u[i][j]); the             } -         } -          for(intI=0; i<n; ++i) { -              for(intj=0; j<m; ++j) { +scanf"%d",&r[i][j]); -             } +         } Amemset (D,0,sizeof(d)); atd[0][0][0]=u[0][0]; -d[1][0][0]=r[0][0]; -          for(intI=0; i<n; ++i) { -              for(intj=0; j<m; ++j) { -                 if(i==0&& j==0)Continue; -                 if(i==0){ in                     intsum=0; -                      for(intk=0; k<=j; ++k) { tosum+=U[i][k]; +                     } -d[0][i][j]=sum; the  *d[1][i][j]=max (d[0][i][j-1],d[1][i][j-1])+R[i][j]; $}Else if(j==0){Panax Notoginsengd[0][i][0]=max (d[0][i-1][0],d[1][i-1][0])+U[i][j]; -  the                     intsum=0; +                      for(intk=0; k<=i; ++k) { Asum+=R[k][j]; the                     } +d[1][i][j]=sum; -}Else{ $                     intsum=0; $                      for(intk=0; k<=j; ++k) { -sum+=U[i][k]; -                     } thed[0][i][j]=max (d[0][i-1][j],d[1][i-1][J]) +sum; - Wuyisum=0; the                      for(intk=0; k<=i; ++k) { -sum+=R[k][j]; Wu                     } -d[1][i][j]=max (d[0][i][j-1],d[1][i][j-1])+sum; About                 } $             } -         } -printf"Case %d:%d\n", Cse,max (d[0][n-1][m-1],d[1][n-1][m-1])); -     } A     return 0; +}

LightOJ1036 A Refining Company (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.