赞
踩
We have a grid with N horizontal rows and N vertical columns.
Let (i,j) denote the square at the i-th row from the top and j-th column from the left. A character
c
i
,
j
c_{i,j}
ci,j describes the color of (i,j).
B means the square is painted black; W means the square is painted white; ? means the square is not yet painted.
Takahashi will complete the black-and-white grid by painting each unpainted square black or white.
Let the zebraness of the grid be the number of pairs of a black square and a white square sharing a side.
Find the maximum possible zebraness of the grid that Takahashi can achieve.
2
BB
BW
2
3
BBB
BBB
W?W
4
5
?????
?????
?????
?????
?????
40
建图+网络流~
对一个大小为
n
∗
n
n*n
n∗n 的矩阵,显然最大值就是
2
∗
n
∗
(
n
−
1
)
2*n*(n-1)
2∗n∗(n−1),那么这题就可以转化成一个最小割(或者最大流),答案就是最大值减去最小割,建图如下:
注意算法复杂度,EK 算法可能会超时,要用 Dicnic 算法,AC代码如下:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 1e5 + 5; #define inf 0x3f3f3f3f struct edge { int from, to; int cap; int flow; edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) { } }; vector<edge> edges; vector<int> G[MAXN]; int vis[MAXN]; int d[MAXN]; int cur[MAXN]; int m; int s, t; void add(int from, int to, int cap) { edges.push_back(edge(from, to, cap, 0)); edges.push_back(edge(to, from, 0, 0)); int m = edges.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); } bool bfs() { memset(vis, 0, sizeof(vis)); queue<int> q; q.push(s); d[s] = 1; vis[s] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < G[x].size(); i++) { edge &e = edges[G[x][i]]; if (!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[x] + 1; q.push(e.to); } } } return vis[t]; } int dfs(int x, int a) { if (x == t || a == 0) return a; int flow = 0, f; for (int &i = cur[x]; i < G[x].size(); i++) { edge &e = edges[G[x][i]]; if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[G[x][i] ^ 1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; } int dicnic() { int ans = 0; while (bfs()) { memset(cur, 0, sizeof(cur)); ans += dfs(s, inf); } return ans; } int main() { int n; cin >> n; vector<string> c(n); for (int i = 0; i < n; i++) cin >> c[i]; s = n * n + 1, t = n * n + 2; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int x = i * n + j; if (c[i][j] == 'B') { if ((i + j) % 2) add(s, x, inf); else add(x, t, inf); } else if (c[i][j] == 'W') { if ((i + j) % 2) add(x, t, inf); else add(s, x, 10); } if (i < n - 1) { int y = (i + 1) * n + j; add(x, y, 1); add(y, x, 1); } if (j < n - 1) { int y = i * n + j + 1; add(x, y, 1); add(y, x, 1); } } } cout << 2 * n * (n - 1) - dicnic(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。