BZOJ 3997: [TJOI2015]组合数学

来源:互联网 发布:软件研发体系建设 编辑:程序博客网 时间:2024/04/29 18:30

Description

给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走。问至少走多少次才能将财宝捡完。此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走多少次才能把财宝全部捡完。

Input

第一行为正整数T,代表数据组数。

每组数据第一行为正整数N,M代表网格图有N行M列,接下来N行每行M个非负整数,表示此格子中财宝数量,0代表没有

Output

输出一个整数,表示至少要走多少次。

Sample Input

1

3 3

0 1 5

5 0 0

1 0 0

Sample Output

10

HINT

N<=1000,M<=1000.每个格子中财宝数不超过10^6

分析

貌似有个定理说在一个DAG中,最小链覆盖=最大独立集(这题不知道这个也行)
观察这个网格图,因为每次只能向右或向下走,所以如果一对点,一个在另一个的左下方,那么走这两个点的最优答案就是他们的值的和
那么在这个图里找到若干个点,满足上述要求,即走到一个点不能到达另一个点,这些点权和最大时就是这个图的答案(因为只要走了这些点,图上的其他任意点都可达并能取完点权),然后DP一下…
其实就是上面那个定理

代码

#include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#include <queue>#include <cmath>#include <map>#include <set>#define N 1010#define INF 0x7fffffff#define sqr(x) ((x) * (x))#define pi acos(-1)int mp[N][N];int f[N][N];int read(){    int x = 0, f = 1;    char ch = getchar();    while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();}    while (ch >= '0' && ch <= '9')  {x = x * 10 + ch - '0'; ch = getchar();}    return x * f;}int main(){    int T = read();    int n,m;    int ans;    while (T--)    {        ans = 0;        n = read(); m = read();        for (int i = 1; i <= n; i++)            for (int j = 1; j <= m; j++)                mp[i][j] = read();        memset(f,0,sizeof(f));        for (int j = 1; j <= n; j++)        {            for (int i = m; i >= 1; i--)                f[i][j] = std::max(f[i][j - 1], f[i + 1][j - 1] + mp[i][j]);            for (int i = m; i >= 1; i--)                f[i][j] = std::max(f[i][j], f[i + 1][j]);            ans = std::max(ans, f[1][j]);        }        printf("%d\n",ans);    }}
0 0