Address
https://www.lydsy.com/JudgeOnline/problem.php?id=3997
Solution
发现题目中走的方式是一个 DAG 最小路径覆盖的模型。
Dilworth 定理:
DAG 的最长反链等于最小路径覆盖。
反链是指图中两两不能到达的点集。
而此网格图中
x,y
x
,
y
两点互相不可到达,等价于
y
y
在 的严格左下方或严格右上方。
问题转化为:在网格中选一些点,使得任意两个点
x,y
x
,
y
都满足上面的限制,求选出点权和的最大值。
我们有了一个 dp 模型:
f[i][j]
f
[
i
]
[
j
]
表示前
i
i
行后 列能取到的最大点权和。
转移就比较显然了:
Code
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)
#define Rof(i, a, b) for (i = a; i >= b; i--)
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
typedef long long ll;
const int N = 1005;
int n, m, a[N][N];
ll f[N][N];
void work() {
int i, j;
n = read(); m = read();
For (i, 1, n) For (j, 1, m) a[i][j] = read();
For (i, 1, n) f[i][m + 1] = 0;
For (i, 1, n) Rof (j, m, 1)
f[i][j] = max(max(f[i - 1][j], f[i][j + 1]),
f[i - 1][j + 1] + a[i][j]);
printf("%lld\n", f[n][1]);
}
int main() {
int T = read();
while (T--) work();
return 0;
}