题意:给你一个n*n的棋盘, 每个格子上有一枚棋子(黑色或白色), 有n*n/2对位置可以交换各自的棋子, 保证每一对位置都在同一行或都在同一列上, 即每一个位置都有一个与之相对应的唯一的位置可以交换。 设第i行黑棋个数为r[i], 第j列黑棋个数为c[j], 求使得棋盘满足 ∀1≤i≤nrl[i]≤r[i]≤rh[i],cl[i]≤c[i]≤ch[i] 的最少移动步数, 无解输出-1。(多组数据 ≤100 , n≤100 , n为偶数)
思路: 考虑带上下界费用流的构图。 将每一行对应点编号为1~n, 每一列对应点编号为n+1 ~ 2n。 考虑初始局面, 对于第i行, 连一条s -> i的上下界均为r[i]费用为0的边, 列情况同理。 考虑目标局面, 对于第i行, 连一条i->t的上界为rl[i]下界为rh[i]费用为0的边 , 列情况同理。 考虑状态的转移, 对于某一对可以交换的位置, 如果原本其上的点颜色相同则直接无视, 假设(x1, y1) (x2, y2)是一对可以交换的位置, 且位于同一列即y1==y2, (x1, y1)上的棋子为黑, 则连一条x1 -> x2容量为1费用为1的边, 其他情况同理。
关于如何实现带上下界的费用流。 新增一个超级源ss, 与超级汇st, 对于一条u->v下界为a, 上界为b, 费用为c的边。 拆成三条: ss->v容量为a费用为c, u->st容量为a费用为c, u->v容量为b-a费用为c的边。 补充一条t->s的容量为inf费用为0的边。 最后跑ss->st的最普通的费用流即可。 如果ss的所有出边存在没有满流的边则输出-1。
PS:如果要用dijkstra+堆优化来写费用流, 要导入势数组的概念来处理网络流中反向边权为负的问题。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500;
const int M = (int)1e6;
const int inf = 1 << 30;
int n, s, t, ss, st, ans;
int G[N][N], c[N], cl[N], ch[N], r[N], rl[N], rh[N];
int cnt = 1, lst[N], nxt[M], to[M], f[M], w[M];
void add(int u, int v, int flow, int cst){
nxt[++ cnt] = lst[u]; lst[u] = cnt; to[cnt] = v; f[cnt] = flow; w[cnt] = cst;
nxt[++ cnt] = lst[v]; lst[v] = cnt; to[cnt] = u; f[cnt] = 0; w[cnt] = -cst;
}
void add(int u, int v, int mn, int mx, int cst){
add(ss, v, mn, cst); add(u, st, mn, cst); add(u, v, mx - mn, cst);
}
int head, tail, que[M], in[N], dis[N], pre[N];
bool spfa(){
head = tail = 0;
for (int i = 1; i <= st; i ++) in[i] = pre[i] = 0, dis[i] = inf;
dis[ss] = 0; que[++ tail] = ss;
while (head < tail){
int u = que[++ head]; in[u] = 0;
for (int j = lst[u]; j; j = nxt[j]){
int v = to[j];
if (!f[j]) continue;
if (dis[u] + w[j] < dis[v]){
pre[v] = j;
dis[v] = dis[u] + w[j];
if (!in[v]){
in[v] = 1; que[++ tail] = v;
}
}
}
}
return dis[st] != inf;
}
void update(){
int now = st, a = inf;
while (now != ss){
int kk = pre[now];
if (a > f[kk]) a = f[kk];
now = to[kk ^ 1];
}
ans += a * dis[st];
now = st;
while (now != ss){
int kk = pre[now];
f[kk] -= a;
f[kk ^ 1] += a;
now = to[kk ^ 1];
}
}
int main(){
while (scanf("%d", &n) != EOF){
cnt = 1;
memset(lst, 0, sizeof(lst));
s = n * 2 + 1, t = s + 1, ss = t + 1, st = ss + 1;
for (int i = 1; i <= n; i ++) c[i] = r[i] = 0;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++){
scanf("%d", G[i] + j);
r[i] += G[i][j], c[j] += G[i][j];
}
for (int i = 1; i <= n; i ++) scanf("%d %d", rl + i, rh + i);
for (int i = 1; i <= n; i ++) scanf("%d %d", cl + i, ch + i);
for (int i = 1; i <= n; i ++){
add(s, i, r[i], r[i], 0);
add(i, t, rl[i], rh[i], 0);
add(s, n + i, c[i], c[i], 0);
add(n + i, t, cl[i], ch[i], 0);
}
for (int i = 1, x1, y1, x2, y2; i <= n * n / 2; i ++){
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
if (G[x1][y1] == G[x2][y2]) continue;
if (!G[x1][y1]) swap(x1, x2), swap(y1, y2);
if (x1 == x2) add(y1 + n, y2 + n, 1, 1);
if (y1 == y2) add(x1, x2, 1, 1);
}
add(t, s, inf, 0);
ans = 0;
while (spfa()) update();
for (int j = lst[ss]; j; j = nxt[j])
if (f[j]) ans = -1;
printf("%d\n", ans);
}
return 0;
}