组合数学
题目背景:
分析:首先注意,我们可以把原题抽象成为一张DAG,然后每个格子可以走到相邻的下方或者右方的格子,但是当前格子中的每一个点互不联通,然后,我们就可以发现原题就是求取一个DAG的最小链覆盖,然后根据
Dilworth定理(我也不知道这是什么,太蒟蒻······):
DAG的最小链覆盖 = 最大独立点集
然后根据每个格子与右下连边,并且,每个格子中的点互不联通可得,本题就是一个求取从右上到左下的和最大的链的过程,并且,我们需要满足对于任意按顺序的链上的两点,Xi+1 > Xi, Yi+1< Yi(严格满足)
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; inline char read() { static const int IN_LEN = 1024 * 1024; static char buf[IN_LEN], *s, *t; if (s == t) { t = (s = buf) + fread(buf, 1, IN_LEN, stdin); if (s == t) return -1; } return *s++; } template
inline bool R(T &x) { static bool iosig; static char c; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return false; if (c == '-') iosig = true; } for (x = 0; isdigit(c); c = read()) x = (x << 3) + (x << 1) + (c ^ '0'); if (iosig) x = -x; return true; } const int OUT_LEN = 1024 * 1024; char obuf[OUT_LEN], *oh = obuf; inline void writechar(char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; } template
inline void W(T x) { static int buf[30], cnt; if (!x) writechar(48); else { if (x < 0) writechar('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) writechar(buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } const int MAXN = 1000 + 10; const int MAXM = 1000 + 10; int n, m, t; int map[MAXN][MAXM], dp[MAXN][MAXM]; inline void readin() { R(n), R(m); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) R(map[i][j]); } /*求取最长的链,从右上到左下角,注意这里的链上的数据, 要严格满足Xi < Xi+1, Yi > Yi+1*/ inline void work() { for (int i = 1; i <= n; ++i) for (int j = m; j >= 1; --j) dp[i][j] = max(dp[i - 1][j + 1] + map[i][j], max(dp[i - 1][j], dp[i][j + 1])); W(dp[n][1]), writechar('\n'); } /*所以就是一个5行的dp就好了······*/ int main() { R(t); while (t--) { readin(); work(); } flush(); return 0; }