[BZOJ3997][TJOI2015]组合数学(图论+dp)

Address

https://www.lydsy.com/JudgeOnline/problem.php?id=3997

Solution

发现题目中走的方式是一个 DAG 最小路径覆盖的模型。
Dilworth 定理:
DAG 的最长反链等于最小路径覆盖。
反链是指图中两两不能到达的点集。
而此网格图中 x , y 两点互相不可到达,等价于 y x 严格左下方或严格右上方。
问题转化为:在网格中选一些点,使得任意两个点 x , y 都满足上面的限制,求选出点权和的最大值。
我们有了一个 dp 模型:
f [ i ] [ j ] 表示前 i 行后 m j + 1 列能取到的最大点权和。
转移就比较显然了:

f [ i ] [ j ] = max ( f [ i 1 ] [ j + 1 ] + v a l [ i ] [ j ] , f [ i 1 ] [ j ] , f [ i ] [ j + 1 ] )

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;
}

猜你喜欢

转载自blog.csdn.net/xyz32768/article/details/81283607
今日推荐