Going Home

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25156   Accepted: 12597

Description

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man. 

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point. 


You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

 

         一堆m要分别走到一间H中,求最小的步数。

         很久以前用二分做过,用二分简练的很,最近联系了最小费用最大流,莫名的有种熟悉的感觉~~(可能我spfa熟练的缘故?)

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 100005;
const int maxm = 1000500;
const int inf = 0x3f3f3f3f;
struct *** {
	int v, ne, cap, flow, cost, u;
}ed[maxm];
int head[maxn], cnt;
int pre[maxn], dis[maxn];
bool vis[maxn];
int n, m;
void init() {
	cnt = 0;
	memset(head, -1, sizeof(head));
}
void add(int u, int v, int cap, int cost) {
	ed[cnt].v = v; ed[cnt].cap = cap; ed[cnt].flow = 0; ed[cnt].u = u;
	ed[cnt].cost = cost; ed[cnt].ne = head[u]; head[u] = cnt++;
	ed[cnt].v = u; ed[cnt].cap = 0; ed[cnt].flow = 0; ed[cnt].u = v;
	ed[cnt].cost = -cost; ed[cnt].ne = head[v]; head[v] = cnt++;
}
bool spfa(int st, int en) {
	queue<int>q;
	for (int s = 0; s <= n; s++) {//这里
		dis[s] = inf; vis[s] = 0; pre[s] = -1;
	}
	dis[st] = 0;
	vis[st] = 1;
	q.push(st);
	while (q.size()) {
		int u = q.front();q.pop();
		vis[u] = 0;
		for (int s = head[u]; ~s; s = ed[s].ne) {
			int v = ed[s].v;
			if (ed[s].cap > ed[s].flow&&dis[v] > dis[u] + ed[s].cost) {
				dis[v] = dis[u] + ed[s].cost;
				pre[v] = s;
				if (!vis[v]) {
					vis[v] = 1; q.push(v);
				}
			}
		}
	}
	if (pre[en] == -1)return 0;
	return 1;
}
int MinMax(int st, int en, int &cost) {
	int flow = 0;
	cost = 0;
	while (spfa(st, en)) {
		int min = inf;
		for (int s = pre[en]; ~s; s = pre[ed[s ^ 1].v]) {
			if (min > ed[s].cap - ed[s].flow)
				min = ed[s].cap - ed[s].flow;
		}
		for (int s = pre[en]; ~s; s = pre[ed[s ^ 1].v]) {
			ed[s].flow += min;
			ed[s ^ 1].flow -= min;
			cost += ed[s].cost*min;
		}
		flow += min;
	}
	return flow;
}
char map[1005][1005];
struct ***er {
	int x, y;
}peo[maxn], hou[maxn];
int Mahatn(int a, int b) {
	return abs(peo[a].x - hou[b].x) + abs(peo[a].y - hou[b].y);
}
int main() {
	ios::sync_with_stdio(0);
	while (~scanf("%d%d", &n, &m) && n + m) {
		//别忘了修改n
		init();
		int scnt = 1, hcnt = 1;
		for (int s = 1; s <= n; s++)
			for (int w = 1; w <= m; w++) {
				cin >> map[s][w];
				if (map[s][w] == 'm')peo[scnt].x = s, peo[scnt++].y = w;
				if (map[s][w] == 'H')hou[hcnt].x = s, hou[hcnt++].y = w;
			}
		scnt--; hcnt--;
		int end = 2 * scnt + 1;
		for (int s = 1; s <= scnt; s++)
			for (int w = 1; w <= scnt; w++)
				add(s, w + scnt, 1, Mahatn(s, w));
		for (int s = 1; s <= scnt; s++)
			add(0, s, 1, 0);
		for (int s = 1; s <= scnt; s++)
			add(s + scnt, end, 1, 0);
		int cost;
		n = end;
		MinMax(0, n, cost);
		cout << cost << endl;
	}
}