POJ 2195 Going Home

最小费用流的裸题

  • 曼哈顿距离
  • 拆点

题目链接 | Here

题意

给出一个$n*m$的字符串,其中$m$代表是人,$H$代表的房子,. 代表立足地,每个人只能上/下/左/右移动。移动一步的花费是1

问: 让所有人移动到房子(每个房子里面只能容纳一个人)里面的最小花费是多少?

输入

多组输入,以0 0为结束标志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2 2         //n m
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0 //退出

输出

1
2
3
2
10
28

题解


曼哈顿距离: $dist(1,2) = |x_1-x_2| + |y_1 - y_2|$


我们发现这题的移动方式为上下左右,由于要求的是最小花费,所以每个人到每个房子的距离必定是曼哈顿距离,那么我们枚举到每个房子的距离曼哈顿距离作为费用容量为1,然后建图跑最小费用最大流即可,由于每个房子最多只能放一个人,所以我们对房子拆点即可。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
using namespace std;

struct ND {
int x,y;
ND(){}
ND(int x,int y):x(x),y(y) {}
};

string l[120];
vector<ND> man,house;

//tmplate_Use
//mcmf(s, t) return MaxFlow
//ret means MinCost
//addedge(u, v, w, c)

const int N = 5e3 + 5, M = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, tot = 1, lnk[N], cur[N], ter[M], nxt[M], cap[M], cost[M], dis[N], ret;
bool vis[N];

void add(int u, int v, int w, int c) {
ter[++tot] = v, nxt[tot] = lnk[u], lnk[u] = tot, cap[tot] = w, cost[tot] = c;
}
void addedge(int u, int v, int w, int c) { add(u, v, w, c), add(v, u, 0, -c); }
bool spfa(int s, int t) {
memset(dis, 0x3f, sizeof(dis));
memcpy(cur, lnk, sizeof(lnk));
std::queue<int> q;
q.push(s), dis[s] = 0, vis[s] = 1;
while (!q.empty()) {
int u = q.front();
q.pop(), vis[u] = 0;
for (int i = lnk[u]; i; i = nxt[i]) {
int v = ter[i];
if (cap[i] && dis[v] > dis[u] + cost[i]) {
dis[v] = dis[u] + cost[i];
if (!vis[v]) q.push(v), vis[v] = 1;
}
}
}
return dis[t] != INF;
}
int dfs(int u, int t, int flow) {
if (u == t) return flow;
vis[u] = 1;
int ans = 0;
for (int &i = cur[u]; i && ans < flow; i = nxt[i]) {
int v = ter[i];
if (!vis[v] && cap[i] && dis[v] == dis[u] + cost[i]) {
int x = dfs(v, t, std::min(cap[i], flow - ans));
if (x) ret += x * cost[i], cap[i] -= x, cap[i ^ 1] += x, ans += x;
}
}
vis[u] = 0;
return ans;
}
int mcmf(int s, int t) {
int ans = 0;
while (spfa(s, t)) {
int x;
while ((x = dfs(s, t, INF))) ans += x;
}
return ans;
}

int main() {
int h,w;
while(1) {
cin >> h >> w;
if(!(h||w)) break;
//Init
man.clear(); house.clear();
memset(lnk,0,sizeof(lnk));
for(int i=0; i<h; i++) {
cin >> l[i];
for(int j=0; j<w; j++) {
if(l[i][j]=='m') man.push_back(ND(i,j));
else if(l[i][j]=='H') house.push_back(ND(i,j));
}
}
int msize = man.size(),hsize = house.size();
int s = (msize+hsize)*2+8,t = (msize+hsize)*2+9;
//s -> man
for(int i=0; i<msize; i++) addedge(s,i,1,0);
//t
for(int i=0; i<hsize; i++) {
addedge(msize+i,msize+hsize+i,1,0);
addedge(msize+hsize+i,t,INF,0);
}
//link
for(int i=0; i<msize; i++) {
ND& mm = man[i];
for(int j=0; j<hsize; j++) {
ND& hh = house[j];
int cost = abs(mm.x - hh.x) + abs(mm.y - hh.y);
addedge(i,j+msize,1,cost);
}
}
ret = 0;
mcmf(s,t);
cout << ret << endl;
}
return 0;
}


--------------------------END--------------------------
喜欢的话,不妨请我喝杯奶茶(≧∇≦)ノ