Bzoj2132: 圈地计划

题面戳我

Solution

一般这种有两种选择的题都可以转化成最小割来做
所以我们先把所有的代价累加,求最小损失

考虑第一二种代价,分S,T连就好了。。。
连完你会发现,第三种怎么连???
要求在同一块儿的损失,怎么用连边表示??

这个时候只能Orz Zsy大佬了
不同类相邻的格子
看到相邻我们想到黑白染色
那么S只连黑色的某区的边和白色的不同区的边,T相反
相邻的黑白点互连C的边,就可以直接跑了
也就大佬想得到吧

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
# define Copy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(100010), __(1e6 + 10), INF(2147483647);

IL ll Read(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, ans, id[110][110], num, val[4][110][110];
int w[__], fst[_], nxt[__], to[__], cnt, S, T, lev[_], cur[_];
queue <int> Q;

IL void Add(RG int u, RG int v, RG int f, RG int _f){
    w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
    w[cnt] = _f; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
}

IL int Dfs(RG int u, RG int maxf){
    if(u == T) return maxf;
    RG int ret = 0;
    for(RG int &e = cur[u]; e != -1; e = nxt[e]){
        if(lev[to[e]] != lev[u] + 1 || !w[e]) continue;
        RG int f = Dfs(to[e], min(w[e], maxf - ret));
        ret += f; w[e ^ 1] += f; w[e] -= f;
        if(ret == maxf) break;
    }
    return ret;
}

IL bool Bfs(){
    Fill(lev, 0); lev[S] = 1; Q.push(S);
    while(!Q.empty()){
        RG int u = Q.front(); Q.pop();
        for(RG int e = fst[u]; e != -1; e = nxt[e]){
            if(lev[to[e]] || !w[e]) continue;
            lev[to[e]] = lev[u] + 1;
            Q.push(to[e]);
        }
    }
    return lev[T];
}

int main(RG int argc, RG char* argv[]){
	n = Read(); m = Read(); Fill(fst, -1); T = n * m + 1;
	for(RG int i = 1; i <= n; ++i)
		for(RG int j = 1; j <= m; ++j)
			id[i][j] = ++num;
	for(RG int g = 1; g <= 3; ++g)
		for(RG int i = 1; i <= n; ++i)
			for(RG int j = 1; j <= m; ++j){
				val[g][i][j] = Read();
				if(g != 3) ans += val[g][i][j];
				else{
					if(i > 1) ans += val[g][i][j];
					if(j > 1) ans += val[g][i][j];
					if(i < n) ans += val[g][i][j];
					if(j < m) ans += val[g][i][j];
				}
			}
	for(RG int i = 1; i <= n; ++i)
		for(RG int j = 1; j <= m; ++j)
			if((j + i) & 1)	Add(S, id[i][j], val[1][i][j], 0), Add(id[i][j], T, val[2][i][j], 0);
			else{
				Add(S, id[i][j], val[2][i][j], 0);
				Add(id[i][j], T, val[1][i][j], 0);
				if(i > 1) Add(id[i][j], id[i - 1][j], val[3][i][j] + val[3][i - 1][j], val[3][i][j] + val[3][i - 1][j]);
				if(j > 1) Add(id[i][j], id[i][j - 1], val[3][i][j] + val[3][i][j - 1], val[3][i][j] + val[3][i][j - 1]);
				if(i < n) Add(id[i][j], id[i + 1][j], val[3][i][j] + val[3][i + 1][j], val[3][i][j] + val[3][i + 1][j]);
				if(j < m) Add(id[i][j], id[i][j + 1], val[3][i][j] + val[3][i][j + 1], val[3][i][j] + val[3][i][j + 1]);			
			}
    while(Bfs()) Copy(cur, fst), ans -= Dfs(S, INF);
	printf("%d\n", ans);
    return 0;
}

posted @ 2018-01-08 17:18  Cyhlnj  阅读(181)  评论(0编辑  收藏  举报