方格取数问题

题目描述

题解:

裸的最小割。

相邻的块不能被同时选中,所以需要将棋盘分成两类。

一类是$x+y$为奇,一类是$x+y$为偶。

然后一类与$S$建边,一类与$T$建边。

最后最大流。用总和减去最大流(即最小割)即为答案。

代码:

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 150
#define ll long long
const int inf = 0x3f3f3f3f;
const ll  Inf = 0x3f3f3f3f3f3f3f3fll;
inline int rd()
{
    int f=1,c=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=10*c+ch-'0';ch=getchar();}
    return f*c;
}
int n,m,hed[N*N],cur[N*N],cnt=-1,S,T;
ll sum;
int _id(int x,int y)
{
    return (x-1)*m+y;
}
struct EG
{
    int to,nxt;
    ll w;
}e[N*N*20];
void ae(int f,int t,ll w)
{
    e[++cnt].to = t;
    e[cnt].nxt = hed[f];
    e[cnt].w = w;
    hed[f] = cnt;
}
int dep[N*N];
bool vis[N*N];
queue<int>q;
bool bfs()
{
    memset(dep,0x3f,sizeof(dep));
    memcpy(cur,hed,sizeof(cur));
    dep[S]=0,vis[S]=1;q.push(S);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int j=hed[u];~j;j=e[j].nxt)
        {
            int to = e[j].to;
            if(e[j].w&&dep[to]>dep[u]+1)
            {
                dep[to] = dep[u]+1;
                if(!vis[to])
                {
                    vis[to] = 1;
                    q.push(to);
                }
            }
        }
        vis[u] = 0;
    }
    return dep[T]!=inf;
}
ll dfs(int u,ll lim)
{
    if(u==T||!lim)return lim;
    ll fl=0,f;
    for(int j=cur[u];~j;j=e[j].nxt)
    {
        cur[u] = j;
        int to = e[j].to;
        if(dep[to]==dep[u]+1&&(f=dfs(to,min(lim,e[j].w))))
        {
            fl+=f,lim-=f;
            e[j].w-=f,e[j^1].w+=f;
            if(!lim)break;
        }
    }
    return fl;
}
ll dinic()
{
    ll ret = 0;
    while(bfs())ret+=dfs(S,Inf);
    return ret;
}
int main()
{
//    freopen("testdata.in","r",stdin);
    n = rd(),m = rd();
    S = n*m+1,T = n*m+2;
    memset(hed,-1,sizeof(hed));
    for(int i=1;i<=n;i++)
        for(int x,y,c,j=1;j<=m;j++)
        {
            c = rd();
            sum+=1ll*c;
            x = _id(i,j);
            if((i+j)&1)
            {
                ae(S,x,c);
                ae(x,S,0);
                if(i!=n)
                {
                    y = _id(i+1,j);
                    ae(x,y,Inf);
                    ae(y,x,0);
                }
                if(j!=m)
                {
                    y = _id(i,j+1);
                    ae(x,y,Inf);
                    ae(y,x,0);
                }
                if(i!=1)
                {
                    y = _id(i-1,j);
                    ae(x,y,Inf);
                    ae(y,x,0);
                }
                if(j!=1)
                {
                    y = _id(i,j-1);
                    ae(x,y,Inf);
                    ae(y,x,0);
                }
            }else
            {
                ae(x,T,c);
                ae(T,x,0);
            }
        }
    printf("%lld\n",sum-dinic());
    return 0;
}

 

posted @ 2019-01-11 17:48  LiGuanlin  阅读(419)  评论(0编辑  收藏  举报