@ljt12138
2017-03-14T14:55:26.000000Z
字数 2428
阅读 816
解题报告
有一个行列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态。要求第i行第j列的格子只能参与次交换。
第一行包含两个整数。以下行为初始状态,每行为一个包含个字符的串,其中表示黑色棋子,表示白色棋子。以下行为目标状态,格式同初始状态。以下n行每行为一个包含个数字的字符串,表示每个格子参与交换的次数上限。
输出仅一行,为最小交换总次数。如果无解,输出。
一眼费用流系列...然而这题的处理有点诡异,首先要拆点将点的限制转化为边的限制,。的权值是多少?分三种情况讨论:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2005;
struct node {
int to, next, f, c, neg;
} edge[MAXN*40];
int head[MAXN], top = 0;
void push(int i, int j, int k, int l)
{
if (i*j == 0) return;
++top, edge[top] = (node) {j, head[i], k, l, top+1}, head[i] = top;
++top, edge[top] = (node) {i, head[j], 0, -l, top-1}, head[j] = top;
}
int vis[MAXN], dis[MAXN], S = 2001, T = 2002;
queue<int> que;
int pre[MAXN], pre_edge[MAXN];
const int INF = 233333333;
bool spfa()
{
memset(dis, 127/3, sizeof dis);
memset(pre, 0, sizeof pre);
vis[S] = 1, que.push(S), dis[S] = 0;
while (!que.empty()) {
int tp = que.front(); que.pop(), vis[tp] = 0;
for (int i = head[tp]; i; i = edge[i].next) {
if (edge[i].f == 0 || dis[edge[i].to] <= dis[tp] + edge[i].c) continue;
int to = edge[i].to, c = edge[i].c;
dis[to] = dis[tp] + c, pre[to] = tp, pre_edge[to] = i;
if (!vis[to])
vis[to] = 1, que.push(to);
}
}
return dis[T] < INF;
}
int sap(int &cost)
{
int ans = INF;
for (int i = T; i != S; i = pre[i]) ans = min(ans, edge[pre_edge[i]].f);
for (int i = T; i != S; i = pre[i]) edge[pre_edge[i]].f -= ans, edge[edge[pre_edge[i]].neg].f += ans;
cost += ans*dis[T];
return ans;
}
int mst(int &cost)
{
cost = 0;
int ans = 0;
while (spfa()) ans += sap(cost);
return ans;
}
int n, m;
int A[30][30], B[30][30], C[30][30];
char str[30];
int readln(int A[30][30])
{
int cnt = 0;
for (int i = 1; i <= n; i++) {
scanf("%s", str+1);
for (int j = 1; j <= m; j++)
A[i][j] = str[j]-'0', cnt += A[i][j];
}
return cnt;
}
inline int number(int i, int j, int id)
{ return (i<=0||i>n||j<=0||j>m)?0:id*n*m+(i-1)*m+j; }
int dx[] = {1,0,-1,0, 1, 1, -1, -1}, dy[] = {0,1,0,-1, -1, 1, -1, 1};
int main()
{
scanf("%d%d", &n, &m);
int a = readln(A);
int b = readln(B); readln(C);
if (a != b) {puts("-1"); return 0;}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (A[i][j] == 1) push(S, number(i, j, 0), 1, 0);
if (B[i][j] == 1) push(number(i, j, 1), T, 1, 0);
if (A[i][j] == 1 && B[i][j] == 1) push(number(i, j, 0), number(i, j, 1), (C[i][j]+2)/2, 1);
else if (A[i][j] == 1 || B[i][j] == 1) push(number(i, j, 0), number(i, j, 1), (C[i][j]+1)/2, 1);
else push(number(i, j, 0), number(i, j, 1), C[i][j]/2, 1);
for (int k = 0; k < 8; k++) push(number(i, j, 1), number(i+dx[k], j+dy[k], 0), INF, 0);
}
int ans = 0, cost = 0;
ans = mst(cost);
if (ans == a) {
cout << cost-a << endl;
} else
puts("-1");
return 0;
}