0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 15:16:28 +00:00

P3159 [CQOI2012]交换棋子

https://www.luogu.com.cn/record/99418216
This commit is contained in:
Baoshuo Ren 2023-01-11 21:50:48 +08:00
parent f9a80c7715
commit b6ee310617
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

189
Luogu/P3159/P3159.cpp Normal file
View File

@ -0,0 +1,189 @@
#include <iostream>
#include <algorithm>
#include <iterator>
#include <queue>
#include <utility>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 25,
M = N * N * 8;
const int INF = 0x3f3f3f3f;
const int d[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
int n, m, s, t, cnt, cnt1, cnt2, cost;
bool g1[N][N], g2[N][N];
int idx, head[M], ver[M << 1], next[M << 1];
std::pair<int, int> edge[M << 1];
int dist[M];
bool vis[M];
int id(int x, int y, int k = 0) {
return n * m * k + (x - 1) * m + y;
}
void add(int u, int v, int c, int w) {
next[idx] = head[u];
ver[idx] = v;
edge[idx] = std::make_pair(c, w);
head[u] = idx++;
}
bool spfa() {
std::fill(std::begin(vis), std::end(vis), false);
std::fill(std::begin(dist), std::end(dist), INF);
std::queue<int> q;
q.emplace(s);
dist[s] = 0;
vis[s] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; ~i; i = next[i]) {
int v = ver[i],
c = edge[i].first,
w = edge[i].second;
if (c > 0 && dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
if (!vis[v]) {
q.push(v);
vis[v] = true;
}
}
}
}
return dist[t] != INF;
}
int dinic(int u, int limit) {
if (u == t) return limit;
int flow = 0;
vis[u] = true;
for (int i = head[u]; ~i && flow < limit; i = next[i]) {
int v = ver[i],
c = edge[i].first,
w = edge[i].second;
if (dist[v] == dist[u] + w && c && !vis[v]) {
int k = dinic(v, std::min(c, limit - flow));
if (!k) dist[v] = INF;
edge[i].first -= k;
edge[i ^ 1].first += k;
flow += k;
cost += k * w;
}
}
vis[u] = false;
return flow;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
std::fill(std::begin(head), std::end(head), -1);
cin >> n >> m;
s = 0, t = id(n, m, 1) + 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c == '1') {
g1[i][j] = true;
cnt1++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c == '1') {
g2[i][j] = true;
cnt2++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g1[i][j] && !g2[i][j]) {
add(s, id(i, j), 1, 0);
add(id(i, j), s, 0, 0);
cnt++;
} else if (!g1[i][j] && g2[i][j]) {
add(id(i, j, 1), t, 1, 0);
add(t, id(i, j, 1), 0, 0);
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
int x = c - '0';
add(id(i, j), id(i, j, 1), x >> 1, 0);
add(id(i, j, 1), id(i, j), 0, 0);
if (g1[i][j] != g2[i][j] && x % 2 == 1) {
add(id(i, j), id(i, j, 1), 1, 0);
add(id(i, j, 1), id(i, j), 0, 0);
}
for (auto d : d) {
int xx = i + d[0],
yy = j + d[1];
if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
add(id(i, j, 1), id(xx, yy), INF, 1);
add(id(xx, yy), id(i, j, 1), 0, -1);
}
}
}
int res = 0;
while (spfa()) {
while (int flow = dinic(s, INF)) res += flow;
}
if (cnt1 != cnt2 || res < cnt) {
cout << -1 << endl;
} else {
cout << cost << endl;
}
return 0;
}