题解:P2499 [SDOI2012] 象棋
Nameless_Face · · 题解
首先不考虑“移动过程中不能出现多颗棋子同时在某一格”的限制,发现问题被转化成了完美匹配,我们只需令左部点到右部点边权为两点间最短路再套一个费用流板子即可。
考虑这个限制,发现我们不仅不在乎操作顺序,还不在乎棋子间差异,于是发现我们可以进行如下操作:若一个棋子按最优解行动时即将与另一个棋子重叠,我们便让另一个棋子按他的方式移动,并让原棋子顶替这枚踏上旅途的棋子的位置,后续按照他的移动方式移动即可。
然后我们就能发现这个限制其实相当于没限制,问题其实就是完美匹配问题,我们只需要预处理每一个点到所有终点的最短距离设为对应费用(建议使用广度优先搜索),然后像板子那样建出超级源汇跑一遍最小费用最大流板子即可。
代码实现比较丑陋,只建议阅读整体实现思路。
#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace user{
int vis[107][107];
}
int n, m, k, a, b, s, t;
struct node{
int x, y;
}l[507];
struct edge{
int to, val, cost;
}ma[520007];
int nxt[520007];
int vis[2007], cur[2007], head[2007];
int tot = 1, ans_f, ans_cost, dis[2007];
void add(int x, int y, int val, int cost){
ma[++tot].to = y;
ma[tot].val = val;
ma[tot].cost = cost;
nxt[tot] = head[x];
head[x] = tot;
}
int id(int bo, int now){
if(bo == 1){
switch(now){
case 1 : case 2 :
return a;
case 3 : case 4 :
return -a;
case 5 : case 6 :
return b;
case 7 : case 8 :
return -b;
}
}else{
switch(now){
case 1 : case 3 :
return b;
case 2 : case 4 :
return -b;
case 5 : case 7 :
return a;
case 6 : case 8 :
return -a;
}
}
}
int map_t[107][107], map_m[107][107];
void bfs_len(node s, int p){
memset(user::vis, 0, sizeof(user::vis));
int tot = 0;
queue<node> q;
q.push(s);
if(map_t[s.x][s.y]){
add(p, map_t[s.x][s.y] + k, 1, 0);
add(map_t[s.x][s.y] + k, p, 0, 0);
tot++;
}
user::vis[s.x][s.y] = 0;
while(!q.empty()){
node u = q.front();
q.pop();
for(int i = 1;i <= 8;++i){
node v;
v.x = u.x + id(1, i);
v.y = u.y + id(2, i);
if(v.x > n){
continue;
}else{
if(v.y > m){
continue;
}else{
if(v.x <= 0){
continue;
}else{
if(v.y <= 0){
continue;
}else{
if(map_m[v.x][v.y]){
continue;
}else{
if(user::vis[v.x][v.y]){
continue;
}
}
}
}
}
}
q.push(v);
user::vis[v.x][v.y] = user::vis[u.x][u.y] + 1;
if(map_t[v.x][v.y]){
add(p, map_t[v.x][v.y] + k, 1, user::vis[v.x][v.y]);
add(map_t[v.x][v.y] + k, p, 0, -user::vis[v.x][v.y]);
tot++;
}
if(tot == k){
return;
}
}
}
}
int spfa(){
for(int i = 0;i <= k * 2 + 1;++i){
dis[i] = 1e9;
vis[i] = 0;
}
queue<int> q;
dis[s] = 0;
vis[s] = 1;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i = head[u];i;i = nxt[i]){
int v = ma[i].to;
if(ma[i].val){
if(dis[v] > dis[u] + ma[i].cost){
dis[v] = dis[u] + ma[i].cost;
if(!vis[v]){
vis[v] = 1;
q.push(v);
}
}
}
}
}
return dis[t] != (1e9);
}
int dfs(int u, int lim){
if(u == t || !lim){
return lim;
}
vis[u] = 1;
int a = 0;
for(int i = cur[u];i;i = nxt[i]){
cur[u] = i;
int v = ma[i].to;
if(!vis[v] && ma[i].val && dis[v] == dis[u] + ma[i].cost){
int f = dfs(v, min(lim, ma[i].val));
if(f){
ma[i].val -= f;
ma[i ^ 1].val += f;
ans_cost += f * ma[i].cost;
lim -= f;
a += f;
if(!lim){
break;
}
}
}
}
vis[u] = 0;
return a;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k >> a >> b;
s = 0;
t = k * 2 + 1;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= m;++j){
char c;
cin >> c;
map_m[i][j] = (c == '.' ? 0 : 1);
}
}
for(int i = 1;i <= k;++i){
cin >> l[i].x >> l[i].y;
}
for(int i = 1;i <= k;++i){
int x, y;
cin >> x >> y;
map_t[x][y] = i;
}
for(int i = 1;i <= k;++i){
bfs_len(l[i], i);
}
for(int i = 1;i <= k;++i){
add(s, i, 1, 0);
add(i, s, 0, 0);
add(i + k, t, 1, 0);
add(t, i + k, 0, 0);
}
while(spfa()){
for(int i = 0;i <= k * 2 + 1;++i){
cur[i] = head[i];
}
ans_f += dfs(s, 0x7fffffff);
}
cout << ans_cost;
return 0;
}