BZOJ 2131 圈地计划(最小割+黑白染色)

类似于happiness的一道题,容易想到最小割的做法。

但是不同的是那一道题是相邻的如果相同则有收益,这题是相邻的不同才有收益。

转化到建图上面时,会发现,两个相邻的点连的边容量会是负数。。

有一种转化的办法,把图进行黑白染色后,把白点的S-T倒转过来,这样就转化成了happiness那道题了。。。

 

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-8
# define MOD 30031
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int N=10005;
//Code begin...

struct Edge{int p, next, w;}edge[N*20];
int head[N], cnt=2, A[105][105], B[105][105], C[105][105];
int s, t, vis[N], n, m, ps[4][2]={0,1,1,0,0,-1,-1,0};
queue<int>Q;

void add_edge(int u, int v, int w){
    edge[cnt].p=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;
    edge[cnt].p=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++;
}
int get_xy(int x, int y){return (x-1)*m+y;}
int bfs(){
    int v;
    mem(vis,-1); vis[s]=0; Q.push(s);
    while (!Q.empty()) {
        v=Q.front(); Q.pop();
        for (int i=head[v]; i; i=edge[i].next) {
            if (edge[i].w>0&&vis[edge[i].p]==-1) {
                vis[edge[i].p]=vis[v]+1;
                Q.push(edge[i].p);
            }
        }
    }
    return vis[t]!=-1;
}
int dfs(int x, int low){
    int a, temp=low;
    if (x==t) return low;
    for (int i=head[x]; i; i=edge[i].next) {
        if (edge[i].w>0&&vis[edge[i].p]==vis[x]+1) {
            a=dfs(edge[i].p,min(edge[i].w,temp));
            temp-=a; edge[i].w-=a; edge[i^1].w+=a;
            if (temp==0) break;
        }
    }
    if (temp==low) vis[x]=-1;
    return low-temp;
}
int dinic(){
    int sum=0;
    while (bfs()) sum+=dfs(s,INF));
    return sum;
}
int main ()
{
    int sum=0;
    scanf("%d%d",&n,&m); s=0; t=n*m+1;
    FOR(i,1,n) FOR(j,1,m) scanf("%d",&A[i][j]), sum+=A[i][j];
    FOR(i,1,n) FOR(j,1,m) scanf("%d",&B[i][j]), sum+=B[i][j];
    FOR(i,1,n) FOR(j,1,m) scanf("%d",&C[i][j]);
    FOR(i,1,n) FOR(j,1,m) {
        if ((i-j)%2==0) add_edge(s,get_xy(i,j),A[i][j]), add_edge(get_xy(i,j),t,B[i][j]);
        else add_edge(s,get_xy(i,j),B[i][j]), add_edge(get_xy(i,j),t,A[i][j]);
        FO(k,0,4) {
            int dx=i+ps[k][0], dy=j+ps[k][1];
            if (dx<=0||dy<=0||dx>n||dy>m) continue;
            add_edge(get_xy(i,j),get_xy(dx,dy),C[i][j]+C[dx][dy]);
            sum+=C[i][j];
        }
    }
    printf("%d\n",sum-dinic());
    return 0;
}
View Code

 

posted @ 2017-05-22 15:03  free-loop  阅读(226)  评论(0编辑  收藏  举报