[清华集训2017]无限之环[费用流、匹配]

题意

题目链接

分析

  • 容易想到将整幅图黑白染色,然后位于黑色格子的管道和位于白色格子的管道要一一匹配。
  • 将每个点拆成五个点,其中四个点表示格子朝上下左右四个方向的管道,一个用来控制流量。
  • 发现旋转 \(90°​\) 这个操作在 可旋转的的管道类型中 都满足可以用 某个方向的管道的位置转移 来表示。
  • 所以把对应方向的管道连边,费用设置成 1 来表示一次旋转即可。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define go(u) for(int i = head[u], v = e[i].to; i; i=e[i].lst, v=e[i].to)
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define pb push_back
#define re(x) memset(x, 0, sizeof x)
inline int gi() {
    int x = 0,f = 1;
    char ch = getchar();
    while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar();}
    while(isdigit(ch)) { x = (x << 3) + (x << 1) + ch - 48; ch = getchar();}
    return x * f;
}
template <typename T> inline bool Max(T &a, T b){return a < b ? a = b, 1 : 0;}
template <typename T> inline bool Min(T &a, T b){return a > b ? a = b, 1 : 0;}
const int N = 2007, inf = 0x3f3f3f3f;
int n, m, gg;
int id[5][N][N];
int dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
namespace MCMF {
	const int Nd = N * 5;
	int edc = 1, st, ed, ndc;
	int head[Nd], cur[Nd], dis[Nd], vis[Nd];
	struct edge {
		int lst, to, cap, flow, c;
	}e[Nd * 20];
	void Add(int a, int b, int c, int d) {
		e[++edc] = (edge){ head[a], b, c, 0, d}, head[a] = edc;
		e[++edc] = (edge){ head[b], a, 0, 0, -d}, head[b] = edc;
	}
	void Add2(int a, int b, int c, int d) {
		e[++edc] = (edge){ head[a], b, c, 0, d}, head[a] = edc;
		e[++edc] = (edge){ head[b], a, 0, 0, -d}, head[b] = edc;
		
		e[++edc] = (edge){ head[b], a, c, 0, d}, head[b] = edc;
		e[++edc] = (edge){ head[a], b, 0, 0, -d}, head[a] = edc;
	}
	bool spfa() {
		queue<int>Q;
		rep(i, 1, ndc) {
			cur[i] = head[i];
			dis[i] = inf;
		}
		dis[st] = 0;
		Q.push(st);
		while(!Q.empty()) {
			int u = Q.front();Q.pop();
			vis[u] = 0;
			go(u)if(e[i].cap - e[i].flow > 0 && Min(dis[v], dis[u] + e[i].c)) {
				if(!vis[v]) vis[v] = 1, Q.push(v);
			}
		}
		return dis[ed] != inf;
	}
	typedef pair<int, int> pii;
	pii dfs(int u, int b) {
		if(u == ed || !b) return make_pair(b, 0);
		pii res, f;
		vis[u] = 1;
		for(int &i = cur[u], v = e[i].to; i; i = e[i].lst, v = e[i].to) 
		if(e[i].cap - e[i].flow > 0 && dis[u] + e[i].c == dis[v] && !vis[v]){
			f = dfs(v, min(b, e[i].cap - e[i].flow));
			res.first += f.first;
			res.second += f.second + e[i].c * f.first;
			b -= f.first, e[i].flow += f.first, e[i ^ 1].flow -= f.first;
			if(!b) break;
		}
		vis[u] = 0;
		return res;
	}
	int MincostMaxflow() {
		pii res = make_pair(0, 0);
		while(spfa()) {
			pii tmp = dfs(st, inf);
			res.second += tmp.second;
			res.first += tmp.first;
		}
		if(res.first * 2 != gg) return -1;
		return res.second;
	}
}
bool inside(int r, int c) {
	return r && c && r <= n && c <= m;
}
int main() {
	using namespace MCMF;
	n = gi(), m = gi();
	rep(i, 1, n) rep(j, 1, m) rep(d, 0, 4) id[d][i][j] = ++ndc;
	st = ++ndc, ed = ++ndc;
	rep(i, 1, n) rep(j, 1, m) {
		int x = gi(), cnt = 0;
		if((i + j) & 1) {
			rep(d, 0, 3) if(x >> d & 1) Add(id[4][i][j], id[d][i][j], 1, 0), ++cnt;
			rep(d, 0, 3) {
				int tr = i + dr[d], tc = j + dc[d];
				if(inside(tr, tc)) Add(id[d][i][j], id[(d + 2) % 4][tr][tc], 1, 0);
			}
			Add(st, id[4][i][j], cnt, 0);
			gg += cnt;
		}else {
			rep(d, 0, 3) if(x >> d & 1) Add(id[d][i][j], id[4][i][j], 1, 0), ++cnt;
			Add(id[4][i][j], ed, cnt, 0);
			gg += cnt;
		}
		if(cnt ^ 2 || (x & 5) || (x & 10)) {
			if(cnt == 1 || cnt == 3) {
				rep(d, 0, 3)
					Add2(id[d][i][j], id[(d + 1) % 4][i][j], 1, 1);
			}
			if(cnt == 2) {
				rep(d, 0, 3) 
					Add2(id[d][i][j], id[(d + 2) % 4][i][j], 1, 1);
			}
		}
	}
	printf("%d\n", MincostMaxflow());
	return 0;
}
posted @ 2019-03-22 17:21  fwat  阅读(202)  评论(0编辑  收藏  举报