想了很久都想不出怎么dp,然后发现有些例子,如果你开始不确定起始值的话,是不能dp的,每种状态都有可能,所以只能二分一个答案,确定开始的val值,来dp了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn=+;
LL dp[maxn][maxn];
int n,m;
int a[maxn][maxn];
bool check (LL val)
{
dp[][]=val;
for (int i=;i<=n;++i)
{
for (int j=;j<=m;++j)
{
if (i== && j==)continue;
if (dp[i-][j]<=) dp[i-][j]=-inf; //如果小于0,证明已经不能走了
if (dp[i][j-]<=) dp[i][j-]=-inf;
if (dp[i-][j]<= && dp[i][j-]<=) dp[i][j]=-inf;
else dp[i][j]=max(dp[i-][j],dp[i][j-])+a[i][j];
}
}
return dp[n][m]>;
}
void work ()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;++i)
for (int j=;j<=m;++j)
scanf("%d",&a[i][j]);
LL begin=,end=1LL***+;
while (begin<=end)
{
LL mid = (begin+end)>>;
//printf ("%d\n",mid);
if (check(mid))
end=mid-;
else begin=mid+;
}
printf ("%lld\n",begin);
//check(998);
//check(3);
//printf ("%d\n",check(1002));
// printf ("%lld\n",dp[2][4]);
return ;
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
for (int i=;i<=;++i)
{
dp[][i]=-inf;
dp[i][]=-inf;
}
dp[][]=-inf;
dp[][]=;
dp[][]=;
int t;
scanf("%d",&t);
while (t--) work();
return ;
}