Loading

代码-JSOI2015 圈地

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);~i;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;

//ReadWrite
const int BUF=1<<20;
static char buf[BUF],*p1=buf,*p2=buf,obuf[BUF],*p3=obuf;
#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,BUF,stdin),p1==p2)?EOF:*p1++
#define putchar(x) (p3-obuf<BUF)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x)
template<typename item>void read(item &x){
    x=0; item f=1; char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=f;
}
template<typename item>void write(item x){
	if(x<0) x=-x,putchar('-');
	if(x>9) write(x/10); putchar(((x%10)^48));
}
struct Flusher{~Flusher(){fwrite(obuf,p3-obuf,1,stdout);}}flusher;

//Data
const int N=200;
int n,m;

//Flows
const int fN=N*N+2;
int fn,s,t,e[fN];
vector<int> nex,to,fw;
void adde(int u,int v,int w){
    nex.pb(e[u]),to.pb(v),fw.pb(w),e[u]=sz(to)-1;
    nex.pb(e[v]),to.pb(u),fw.pb(0),e[v]=sz(to)-1;
}
int dep[fN],cur[fN],q[fN],*ta,*he;
bool bfs(){
    R(u,fn) dep[u]=-1,cur[u]=e[u];
    ta=he=q-1,dep[*++ta=s]=0;
    while(ta!=he){
        int u=*++he;
        for(int v=e[u];~v;v=nex[v])
            if(fw[v]&&!~dep[to[v]]){
                dep[to[v]]=dep[u]+1;
                if(to[v]==t) return true;
                *++ta=to[v];
            }
    }
    return false;
}
int dfs(int u,int r){
    if(u==t||!r) return r;
    int res=0,f;
    for(int &v=cur[u];~v;v=nex[v])
        if(fw[v]&&dep[to[v]]==dep[u]+1
        &&(f=dfs(to[v],min(r,fw[v])))>0){
            fw[v]-=f,fw[v^1]+=f,r-=f,res+=f;
            if(!r) break;
        }
    if(r) dep[u]=-1;
    return res;
}
int flow(){
    int res=0;
    while(bfs()) res+=dfs(s,iinf);
    return res;
}

//Main
int main(){
    read(n),read(m),fn=(t=(s=n*m)+1)+1;
    R(u,fn) e[u]=-1; int sm=0,a;
    R(i,n)R(j,m) read(a),(a>0)&&(adde(s,i*m+j,a),sm+=a),
        (a<0)&&(adde(i*m+j,t,-a),sm+=-a);
    R(i,n-1)R(j,m) read(a),adde(i*m+j,i*m+m+j,a),adde(i*m+m+j,i*m+j,a);
    R(i,n)R(j,m-1) read(a),adde(i*m+j,i*m+j+1,a),adde(i*m+j+1,i*m+j,a);
    write(sm-flow()),putchar('\n');
    return 0;
}
posted @ 2020-11-18 15:33  George1123  阅读(40)  评论(0编辑  收藏  举报