P4304 [TJOI2013]攻击装置 最小割

$ color{#0066ff}{ 题目描述 }$

给定一个01矩阵,其中你可以在0的位置放置攻击装置。 每一个攻击装置(x,y)都可以按照“日”字攻击其周围的8个位置(x-1,y-2),(x-2,y-1),(x+1,y-2),(x+2,y-1),(x-1,y+2),(x-2,y+1),(x+1,y+2),(x+2,y+1)

求在装置互不攻击的情况下,最多可以放置多少个装置。某地方有N个工厂,有N-1条路连接它们,且它们两两都可达。每个工厂都有一个产量值和一个污染值。现在工厂要进行规划,拆除其中的M个工厂,使得剩下的工厂依然连成一片且 总产量/总污染 的值最大。

(color{#0066ff}{输入格式})

第一行一个整数N,表示矩阵大小为N*N。

接下来N行每一行一个长度N的01串,表示矩阵。

(color{#0066ff}{输出格式})

一个整数,表示在装置互不攻击的情况下最多可以放置多少个装置。

(color{#0066ff}{输入样例})

3
010
000
100

(color{#0066ff}{输出样例})

4

(color{#0066ff}{数据范围与提示})

30%数据N<=50

100%数据 N<=200

(color{#0066ff}{题解})

发现两个互相影响的块的奇偶性不同, 于是黑白染色跑最小割

#include<bits/stdc++.h>
#define LL long long
LL read() {
	char ch; LL x = 0, f = 1;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
	return x * f;
}
template<class T> bool chkmax(T &a, const T &b) { return a < b? a = b, 1 : 0; }
template<class T> bool chkmin(T &a, const T &b) { return b < a? a = b, 1 : 0; }
const int inf = 0x7fffffff;
const int maxn = 1e5 + 10;
struct node {
	int to, can;
	node *nxt, *rev;
	node(int to = 0, int can = 0, node *nxt = NULL): to(to), can(can), nxt(nxt) { rev = NULL; }
};
node *head[maxn], *cur[maxn];
int dep[maxn], n, s, t, mp[400][400];
int rx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int ry[] = {-1, 1, -2, 2, -2, 2, -1, 1};
bool bfs() {
	for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
	std::queue<int> q;
	q.push(s); dep[s] = 1;
	while(!q.empty()) {
		int tp = q.front(); q.pop();
		for(node *i = head[tp]; i; i = i->nxt) 
			if(!dep[i->to] && i->can)
				dep[i->to] = dep[tp] + 1, q.push(i->to);
	}
	return dep[t];
}
int dfs(int x, int change) {
	if(x == t || !change) return change;
	int flow = 0, ls;
	for(node *&i = cur[x]; i; i = i->nxt)
		if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->can)))) {
			change -= ls;
			flow += ls;
			i->can -= ls;
			i->rev->can += ls;
			if(!change) break;
		}
	return flow;
}
int dinic() {
	int flow = 0;
	while(bfs()) flow += dfs(s, inf);
	return flow;
}
int id(int x, int y) { return (x - 1) * n + y; }
void add(int from, int to, int can) { head[from] = new node(to, can, head[from]); }
void link(int from, int to, int can) {
	add(from, to, can), add(to, from, 0);
	head[from]->rev = head[to]; head[to]->rev = head[from];
}
int main() {
	n = read();
	int tot = 0;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) {
			scanf("%1d", &mp[i][j]);
			tot += mp[i][j];
		}
	tot = n * n - tot;
	s = 0, t = n * n + 1;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) {
			if(mp[i][j]) continue;
			if((i + j) & 1) link(id(i, j), t, 1);
			else link(s, id(i, j), 1
				);
			if((i + j) & 1) continue;
			for(int k = 0; k < 8; k++) {
				int xx = i + rx[k];
				int yy = j + ry[k];
				if(xx >= 1 && xx <= n && yy >= 1 && yy <= n && !mp[xx][yy])  link(id(i, j), id(xx, yy), inf);
			}
		}
	printf("%d
", tot - dinic());
	return 0;
}
原文地址:https://www.cnblogs.com/olinr/p/10653427.html