bzoj3997[TJOI2015]组合数学

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-9)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }

const int maxn=1000;

int n,m;
int mp[maxn+10][maxn+10];
LL f[maxn+10][maxn+10];

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,1,n)re(j,1,m)mp[i][j]=gint();
            mmst(f,0);
            re(i,1,n)red(j,m,1)f[i][j]=max(f[i-1][j+1]+mp[i][j],max(f[i][j+1],f[i-1][j]));
            cout<<f[n][1]<<endl;
        }
      return 0;
  }
View Code

 

posted @ 2015-09-29 20:51  maijing  阅读(443)  评论(0编辑  收藏  举报