http://www.lydsy.com/JudgeOnline/problem.php?id=3997
偏序集,看上一篇随笔。
我们要求最少路径覆盖,可以等价于求最大独立集。
我们要找到一个权值和最大的点集$S$,使得对于点集中任意两个点$点i$和$点j$,使得$点i$不能到$点j$,就是要求$点i$严格在$点j$的右上方或左下方。
用DP可以在$O(N^2)$内解决。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b) for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} inline int sgn(DB x){if(abs(x)<1e-)return ;return(x>)?:-;}
const DB Pi=acos(-1.0); int gint()
{
int res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
}
LL gll()
{
LL res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
} const int maxn=; int n,m;
int mp[maxn+][maxn+];
LL f[maxn+][maxn+]; int main()
{
freopen("bzoj3997.in","r",stdin);
freopen("bzoj3997.out","w",stdout);
int i,j;
for(int Case=gint();Case;Case--)
{
n=gint();m=gint();
re(i,,n)re(j,,m)mp[i][j]=gint();
mmst(f,);
re(i,,n)red(j,m,)f[i][j]=max(f[i-][j+]+mp[i][j],max(f[i][j+],f[i-][j]));
cout<<f[n][]<<endl;
}
return ;
}