题意:
在 n * m的矩阵上有 k个人和k所房子,一所房子只能待一个人,人可以上下左右的移动,求让每个人都走到房子里的最小总步数,注意可以经过房子所在的格子不进入房子。
思路:
跑最小费用最大流,人到房子的距离为边权,人房边的容量为1建图,再建立超级源点0连接每个人,边权为0, 边的容量为1,建立超级汇点201连接没所房子,边权为0,边的容量为1,用最大流限制人和房子的一一对应关系,同时用一边权跑最短路,每次找到一条最短路,直到跑完最大流。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 1 << 30;
struct edges{
int u, v, cost, flow, next;
} g[maxn];
int head[205], cnt;
int d[205], last[205], min_flow[205];
int mp[105][105];
void init(int n){
cnt = 0;
for(int i = 0; i <= n; i++){
head[i] = -1;
}
}
void add(int u, int v, int flow, int cost){
g[cnt].u = u;
g[cnt].v = v;
g[cnt].cost = cost;
g[cnt].flow = flow;
g[cnt].next = head[u];
head[u] = cnt++;
}
bool spfa(int n, int s, int t){
bool vis[205];
for(int i = 0; i <= n; i++){
vis[i] = false;
d[i] = inf;
min_flow[i] = inf;
}
queue<int> qu;
while(qu.size()) qu.pop();
qu.push(s);
d[s] = 0;
while(qu.size()){
int u = qu.front();
qu.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = g[i].next){
int v = g[i].v;
if(g[i].flow > 0 && d[v] > d[u] + g[i].cost){
min_flow[v] = min(min_flow[u], g[i].flow);
d[v] = d[u] + g[i].cost;
last[v] = i;
if(!vis[v]) qu.push(v), vis[v] = true;
}
}
}
return (d[t] != inf);
}
int mcfc(int n, int s, int t){
int cost = 0, flow;
while(spfa(n, s, t)){
cost += d[t];
flow = min_flow[t];
int x = t;
while(x != s){
int k = last[x];
g[k].flow -= flow;
g[k ^ 1].flow += flow;
x = g[last[x] ^ 1].v;
}
}
return cost;
}
bool check(int n, int m, int x, int y){
return x < n && x >= 0 && y < m && y >= 0;
}
void find(int n, int m, int u, int cot){
int range[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool vis[105][105];
int pace[105][105];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
vis[i][j] = false, pace[i][j] = 0;
int x = u / m;
int y = u % m;
vis[x][y] = true;
queue<pair<int, int> > qu;
while(qu.size()) qu.pop();
qu.push(make_pair(x, y));
while(qu.size()){
pair<int, int> pa = qu.front();
qu.pop();
for(int i = 0; i < 4; i++){
x = pa.first + range[i][0];
y = pa.second + range[i][1];
if(check(n, m, x, y) && !vis[x][y]) {
pace[x][y] = pace[pa.first][pa.second] + 1;
vis[x][y] = true;
qu.push(make_pair(x, y));
if(mp[x][y] > 100) {
add(cot, mp[x][y], 1, pace[x][y]);
add(mp[x][y], cot, 0, -pace[x][y]);
}
}
}
}
}
int main(){
int n, m;
char s[105];
int indx[105];
while(scanf("%d%d", &n, &m), n && m){
int cnt1 = 1, cnt2 = 101;
for(int i = 0; i < n; i++){
scanf("%s", s);
for(int j = 0; j < strlen(s); j++) {
if(s[j] == 'm') mp[i][j] = cnt1, indx[cnt1++] = i * m + j;
if(s[j] == 'H') mp[i][j] = cnt2++;
if(s[j] == '.') mp[i][j] = -1;
}
}
init(201);
for(int i = 1; i < cnt1; i++)
find(n, m, indx[i], i);
for(int i = 1; i < cnt1; i++){
add(0, i, 1, 0);
add(i, 0, 0, 0);
}
for(int i = 101; i < cnt2; i++){
add(i, 201, 1, 0);
add(201, i, 0, 0);
}
// for(int i = 0; i < cnt; i++) {
// printf("u = %d v = %d cost = %d flow = %d\n", g[i].u, g[i].v, g[i].cost, g[i].flow);
// }
printf("%d\n", mcfc(201, 0, 201));
}
}
HK算法思路:
很明显这也是一个二分图问题,对于带权二分匹配我们可以用HK算法解决,HK算法(用slack数组优化后)的复杂度理论上是 n^3 但我提交时Runtime Error了,这题在时间复杂度上还是最小费用最大流方案更优秀。
code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1e3 + 5;
const int inf = 1 << 30;
struct edegs{
int u, v, w, next;
} g[1005];
int head[maxn], match[maxn], wx[maxn], wy[maxn], slack[maxn], cnt;
bool visx[maxn], visy[maxn];
int mp[105][105], rop[205][205];
void init(int n){
cnt = 0;
for(int i = 0; i <= n; i++){
head[i] = -1;
}
}
void add(int u, int v, int w){
g[cnt].u = u;
g[cnt].v = v;
g[cnt].w = w;
g[cnt].next = head[u];
head[u] = cnt++;
}
bool dfs(int u){
visx[u] = true;
for(int i = head[u]; i != -1; i = g[i].next){
int v = g[i].v;
if(visy[v]) continue;
int t = wx[u] + wy[v] - g[i].w;
if(t == 0){
visy[v] = true;
if(match[v] == -1 || dfs(match[v])){
match[v] = u;
return true;
}
}
if(t > 0) slack[v] = min(t, slack[v]);
}
return false;
}
int HK(int n){
for(int i = 1; i <= n; i++){
wx[i] = 0;
wy[i + 100] = 0;
match[i + 100] = -1;
}
for(int i = 1; i <= n; i++)
for(int j = head[i]; j != -1; j = g[j].next)
wx[i] = max(wx[i], g[j].w);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++)
slack[j + 100] = inf;
while(1){
for(int j = 1; j <= n; j++){
visx[j] = visy[j + 100] = false;
}
if(dfs(i)) break;
int maxz = inf;
for(int j = 1; j <= n; j++)
if(!visy[j + 100]) maxz = min(maxz, slack[j + 100]);
for(int j = 1; j <= n; j++){
if(visx[j]) wx[j] -= maxz;
if(visy[j + 100]) wy[j + 100] += maxz;
else slack[j + 100] -= maxz;
}
}
}
for(int i = 0; i < cnt; i = i + 2){
int u = g[i].u;
int v = g[i].v;
rop[u][v] = rop[v][u] = g[i].w;
}
int ans = 0;
for(int i = 101; i <= 100 + n; i++)
ans += rop[match[i]][i];
return ans;
}
bool check(int n, int m, int x, int y){
return x < n && x >= 0 && y < m && y >= 0;
}
void find(int n, int m, int u, int cot){ //bfs建图
int range[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool vis[105][105];
int pace[105][105];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
vis[i][j] = false, pace[i][j] = 0;
int x = u / m;
int y = u % m;
vis[x][y] = true;
queue<pair<int, int> > qu;
while(qu.size()) qu.pop();
qu.push(make_pair(x, y));
while(qu.size()){
pair<int, int> pa = qu.front();
qu.pop();
for(int i = 0; i < 4; i++){
x = pa.first + range[i][0];
y = pa.second + range[i][1];
if(check(n, m, x, y) && !vis[x][y]) {
pace[x][y] = pace[pa.first][pa.second] + 1;
vis[x][y] = true;
qu.push(make_pair(x, y));
if(mp[x][y] > 100) {
add(cot, mp[x][y], pace[x][y]);
add(mp[x][y], cot, pace[x][y]);
}
}
}
}
}
int main(){
int n, m;
char s[105];
int indx[105];
while(scanf("%d%d", &n, &m), n && m){
init(200);
int cnt1 = 0, cnt2 = 100;
for(int i = 0; i < n; i++){
scanf("%s", s);
for(int j = 0; j < strlen(s); j++){
if(s[j] == 'm') mp[i][j] = ++cnt1, indx[cnt1] = i * m + j;
if(s[j] == 'H') mp[i][j] = ++cnt2;
if(s[j] == '.') mp[i][j] = -1;
}
}
for(int i = 1; i <= cnt1; i++)
find(n, m, indx[i], i);
// for(int i = 0; i < cnt; i++){
// printf("u = %d v = %d w = %d\n", g[i].u, g[i].v, g[i].w);
// }
printf("%d\n", HK(cnt1));
}
}