Analysis of the sample can be found that the actual request is from the lower left to the upper right corner of the longest road
Because for any point that is not on the longest path, the value of this point can be reduced by the way of the point on the longest road.
(Can prove with contradiction)
And then there's a very noip DP.
#include <cstdio> #include <iostream> #include <algorithm> #include <cstdlib> #include < Cstring>using namespace Std; const int Maxn=1010;int t,n,m;int f[maxn][maxn];int map[maxn][maxn];void cmax (int &a,int b) {if (a<b) A=b;return;} void read (int &num) { Num=0;char Ch=getchar (); while (ch< '! ') Ch=getchar (); while (ch>= ' 0 ' &&ch<= ' 9 ') num=num*10+ch-' 0 ', Ch=getchar ();} int main () { read (T); while (t--) { read (n); read (m); for (int. i=1;i<=n;++i) for (int j=1;j<=m;++j) read (map[i][j]); memset (F,0,sizeof (f)); for (int i=n;i>=1;--i) {for (int j=1;j<=m;++j) { Cmax (f[i][j],f[i+1][j]); Cmax (F[i][j],f[i][j-1]); Cmax (F[i][j],f[i+1][j-1]+map[i][j]); } } printf ("%d\n", F[1][m]); } return 0;}
Bzoj 3997 TJOI2015 Combinatorial mathematics