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

Address

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

Solution

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

f[i][j]=max(f[i1][j+1]+val[i][j],f[i1][j],f[i][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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值