题干:

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

题目大意:

在n*m的矩阵格子里有x个人('m')要走到x个房间('H')里,每个房间里只能放一人,人在目前所在的格子处向上下左右相邻的格子走一步就花费1美元,问最后全部的人走到房间后最小的花费。

解题报告:

  直接建图之后最小费用流。

新建超级源点st,超级汇点ed。

st连向每一个m,流量为1,费用为0。

每一个H连向ed,

新增源点from和汇点to,from->人(w为1,cost为0)

房子->to(w为1,cost为0)

每个人->每间房(w为1,cost为最短路径的美元花费)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;

const int MAXN = 70000;
const int MAXM = 100005;
const int INF = 0x3f3f3f3f;
struct Edge {
	int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从 1 ~ N
void init(int n) {
	N = n;
	tol = 0;
	memset(head, -1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost) {
	edge[tol].to = v;
	edge[tol].cap = cap;
	edge[tol].cost = cost;
	edge[tol].flow = 0;
	edge[tol].next = head[u];
	head[u] = tol++;
	edge[tol].to = u;
	edge[tol].cap = 0;
	edge[tol].cost = -cost;
	edge[tol].flow = 0;
	edge[tol].next = head[v];
	head[v] = tol++;
}
bool spfa(int s,int t) {
	queue<int>q;
	for(int i = 1; i <= N; i++) {
		dis[i] = INF;
		vis[i] = false;
		pre[i] = -1;
	}
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = false;
		for(int i = head[u]; i !=-1; i = edge[i].next) {
			int v = edge[i].to;
			if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge
			        [i].cost ) {
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				if(!vis[v]) {
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
	if(pre[t] ==-1)return false;
	else return true;

}
//返回的是最大流,cost 存的是最小费用
int minCostMaxflow(int s,int t,int &cost) {
	int flow = 0;
	cost = 0;
	while(spfa(s,t)) {
		int Min = INF;
		for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {
			if(Min > edge[i].cap-edge[i].flow)
				Min = edge[i].cap-edge[i].flow;
		}
		for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {
			edge[i].flow += Min;
			edge[i^1].flow-= Min;
			cost += edge[i].cost * Min;
		}
		flow += Min;
	}
	return flow;
}
char s[111][111];
int r[111],c[111];
int R[111],C[111];
int tot1,tot2;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)) {
    	if(n == 0 && m == 0) break;
        init(n*m+2);
		memset(dis,0,sizeof dis);
		tot1=tot2=0;
		int st=n*m+1,ed=n*m+2;
        int cost;
        for(int i = 1; i<=n; i++) {
			scanf("%s",s[i]+1);
		}
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=m; j++) {
				if(s[i][j] == 'm') tot1++,r[tot1] = i,c[tot1] = j;
				if(s[i][j] == 'H') tot2++,R[tot2] = i,C[tot2] = j;
			}
		}
		for(int i = 1; i<=tot1; i++) {
			for(int j = 1; j<=tot2; j++) addedge(i,tot1+j,1,abs(c[i]-C[j]) + abs(r[i]-R[j]));
		}
		for(int i = 1; i<=tot1; i++) addedge(st,i,1,0);
		for(int i = 1; i<=tot2; i++) addedge(tot1+i,ed,1,0);
        int ans = minCostMaxflow(st,ed,cost);
        printf("%d\n",cost);
    }	
	return 0 ;
}