2668: [cqoi2012]交换棋子
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1136 Solved: 423
[ Submit][ Status][ Discuss]
Description
有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态。要求第i行第j列的格子只能参与mi,j次交换。
Input
第一行包含两个整数n,m(1<=n, m<=20)。以下n行为初始状态,每行为一个包含m个字符的01串,其中0表示黑色棋子,1表示白色棋子。以下n行为目标状态,格式同初始状态。以下n行每行为一个包含m个0~9数字的字符串,表示每个格子参与交换的次数上限。
Output
输出仅一行,为最小交换总次数。如果无解,输出-1。
Sample Input
3 3
110
000
001
000
110
100
222
222
222
110
000
001
000
110
100
222
222
222
Sample Output
4
HINT
Source
数据范围非常网络流,答案非常费用流,不过不会建模。。
忽略图中的白色棋子,只考虑黑色棋子,问题转变成
将原图中的每个黑色棋子移动成新图的样子,最少需要移动多少步
约束是每个格子有一个经过次数的上限
把某个黑棋子,从格子A移动到格子B,视作从A流出流入B
那么就将一个点i拆成三个点,xi,yi,zi,连(xi,yi)和(yi,zi)两条边
前者用来限制流入后者限制流出
分类讨论图中的每个点(i,j),记A = (xi,yi)的容量,B = (yi,zi)的容量,交换上限为wi,j
如果原图和新图中某个位置颜色相同,说明其流入流出次数应该相等,所以A = B = wi,j / 2
如果原图是黑色而新图是白色,说明应该多流出一次,所以A = wi,j / 2,B = (wi,j + 1) / 2
反之,同理可得A = (wi,j + 1) / 2,B = wi,j / 2
上述除法全部是取下整
构建超级源S,超级汇T
如果某点在原图是黑色,连S到yi,j容量为1的边,代表这个黑棋子从这个点出发
如果某点在新图是黑色,连yi,j到T容量为1的边,代表必须有一个黑棋子在这里停止
最后,根据八连通把x,z之间的边建完,跑个最小费用最大流就行了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1222;
const int maxm = 4E5 + 40;
const int INF = 1E9 + 233;
const int dx[8] = {0,-1,0,1,1,1,-1,-1};
const int dy[8] = {1,0,-1,0,1,-1,1,-1};
struct E{
int to,cap,flow,cost; E(){}
E(int to,int cap,int flow,int cost):
to(to),cap(cap),flow(flow),cost(cost){}
}edgs[maxm];
int n,m,S,T,tot,t1,t2,cnt,Num[22][22],A[22][22],B[22][22],cost[maxn],flow[maxn],from[maxn];
char ch[22],s[22][22],t[22][22];
bool vis[maxn];
vector <int> v[maxn];
queue <int> Q;
void Add(int x,int y,int cap,int cost)
{
v[x].push_back(cnt); edgs[cnt++] = E(y,cap,0,cost);
v[y].push_back(cnt); edgs[cnt++] = E(x,0,0,-cost);
}
bool SPFA()
{
for (int i = S; i <= T; i++) cost[i] = INF;
vis[S] = 1; cost[S] = 0; flow[S] = INF; Q.push(S);
while (!Q.empty())
{
int k = Q.front(); Q.pop(); vis[k] = 0;
for (int i = 0; i < v[k].size(); i++)
{
E e = edgs[v[k][i]];
if (e.cap == e.flow || cost[e.to] <= cost[k] + e.cost) continue;
from[e.to] = v[k][i];
cost[e.to] = cost[k] + e.cost;
flow[e.to] = min(flow[k],e.cap - e.flow);
if (!vis[e.to]) vis[e.to] = 1,Q.push(e.to);
}
}
return cost[T] != INF;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
Num[i][j] = ++tot,A[i][j] = ++tot,B[i][j] = ++tot;
T = ++tot;
for (int i = 1; i <= n; i++)
{
scanf("%s",s[i] + 1);
for (int j = 1; j <= m; j++)
if (s[i][j] == '0') ++t1,Add(S,Num[i][j],1,0);
}
for (int i = 1; i <= n; i++)
{
scanf("%s",t[i] + 1);
for (int j = 1; j <= m; j++)
if (t[i][j] == '0') ++t2,Add(Num[i][j],T,1,0);
}
if (t1 != t2) {cout << -1 << endl; return 0;}
for (int i = 1; i <= n; i++)
{
scanf("%s",ch + 1);
for (int j = 1; j <= m; j++)
{
int now = ch[j] - '0',x,y;
if (s[i][j] == t[i][j]) x = y = now >> 1;
else if (s[i][j] == '0') x = now >> 1,y = now + 1 >> 1;
else x = now + 1 >> 1,y = now >> 1;
Add(A[i][j],Num[i][j],x,0); Add(Num[i][j],B[i][j],y,0);
for (int k = 0; k < 8; k++)
{
x = i + dx[k]; y = j + dy[k];
if (x < 0 || x > n || y < 0 || y > m) continue;
Add(B[i][j],A[x][y],INF,1);
}
}
}
int MaxFlow,Cost; MaxFlow = Cost = 0;
while (SPFA())
{
Cost += cost[T] * flow[T]; MaxFlow += flow[T];
for (int z = T; z != S; z = edgs[from[z]^1].to)
{
edgs[from[z]].flow += flow[T];
edgs[from[z]^1].flow -= flow[T];
}
}
cout << (MaxFlow == t1 ? Cost : -1) << endl;
return 0;
}