[网络流24题][BZOJ1475]方格取数(最小割)

题目描述

传送门

题解

偶然得知来刷水,具体做法见方格取数3.

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

const int max_n=35;
const int max_m=35;
const int max_N=max_n*max_m+2;
const int max_M=max_N*10;
const int max_e=max_M*2;
const int INF=1e9;

int n,m,N,cap,loc,loc1,maxflow,sum;
int tot,point[max_N],next[max_e],v[max_e],remain[max_e];
int deep[max_N],cur[max_N],last[max_N],num[max_N];
queue <int> q;

inline void addedge(int x,int y,int cap){
    ++tot; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;
    ++tot; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}

inline void bfs(int t){
    for (int i=1;i<=N;++i)
      deep[i]=N;
    deep[t]=0;
    for (int i=1;i<=N;++i)
      cur[i]=point[i];
    while (!q.empty()) q.pop();
    q.push(t);

    while (!q.empty()){
        int now=q.front(); q.pop();
        for (int i=point[now];i!=-1;i=next[i])
          if (deep[v[i]]==N&&remain[i^1]){
            deep[v[i]]=deep[now]+1;
            q.push(v[i]);
          }
    }
}

inline int addflow(int s,int t){
    int ans=INF,now=t;
    while (now!=s){
        ans=min(ans,remain[last[now]]);
        now=v[last[now]^1];
    }

    now=t;
    while (now!=s){
        remain[last[now]]-=ans;
        remain[last[now]^1]+=ans;
        now=v[last[now]^1];
    }
    return ans;
}

inline void isap(int s,int t){
    bfs(t);
    for (int i=1;i<=N;++i)
      num[deep[i]]++;

    int now=s;
    while (deep[s]<N){
        if (now==t){
            maxflow+=addflow(s,t);
            now=s;
        }

        bool has_find=false;
        for (int i=cur[now];i!=-1;i=next[i])
          if (deep[now]==deep[v[i]]+1&&remain[i]){
            has_find=true;
            cur[now]=i;
            last[v[i]]=i;
            now=v[i];
            break;
          }

        if (!has_find){
            int minn=N-1;
            for (int i=point[now];i!=-1;i=next[i])
              if (remain[i])
                minn=min(minn,deep[v[i]]);
            if (!(--num[deep[now]])) break;
            num[deep[now]=minn+1]++;
            cur[now]=point[now];
            if (now!=s)
              now=v[last[now]^1];
        }
    }
}

int main(){
    tot=-1;
    memset(point,-1,sizeof(point));
    memset(next,-1,sizeof(next));

    scanf("%d",&n);
    N=n*n+2;
    for (int i=1;i<=n;++i)
      for (int j=1;j<=n;++j){
        scanf("%d",&cap);
        sum+=cap;
        loc=(i-1)*n+j;
        if (i%2)//奇数排 
          if (j%2){//奇点为黑点
            addedge(1,1+loc,cap);
            if (i>1){
                loc1=(i-2)*n+j;
                addedge(1+loc,1+loc1,INF);
            }
            if (j>1){
                loc1=loc-1;
                addedge(1+loc,1+loc1,INF);
            }
            if (i<n){
                loc1=i*n+j;
                addedge(1+loc,1+loc1,INF);
            }
            if (j<n){
                loc1=loc+1;
                addedge(1+loc,1+loc1,INF);
            }
          } 
          else addedge(1+loc,N,cap);//偶点为白点 
        else//偶数 
          if (j%2) addedge(1+loc,N,cap);
          else{
            addedge(1,1+loc,cap);
            if (i>1){
                loc1=(i-2)*n+j;
                addedge(1+loc,1+loc1,INF);
            }
            if (j>1){
                loc1=loc-1;
                addedge(1+loc,1+loc1,INF);
            }
            if (i<n){
                loc1=i*n+j;
                addedge(1+loc,1+loc1,INF);
            }
            if (j<n){
                loc1=loc+1;
                addedge(1+loc,1+loc1,INF);
            }
          } 
      }

    isap(1,N);
    printf("%d\n",sum-maxflow);
}

你可能感兴趣的:(网络流,bzoj)