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.
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.
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.
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
2 2 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0
2 10 28
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define mem(a,b) memset(a,b,sizeof(a))
const int N=1000+20;
const int M=1000+20;
int top,maxflow;//当前边下标,最大流
int dis[N],pre[N];//源点到点i的最小距离,pre[i]记录前驱
bool vis[N];//标记数组
int first[N];//存储头结点
struct Edge
{
int v,next;
int cap,flow,cost;
} E[M*2];
void init()
{
mem(first,-1);
top=0;
maxflow=0;
}
void add_edge(int u,int v,int c,int cost)
{
E[top].v=v;
E[top].cap=c;
E[top].flow=0;
E[top].cost=cost;
E[top].next=first[u];
first[u]=top++;
}
void add(int u,int v,int c,int cost)
{
add_edge(u,v,c,cost);
add_edge(v,u,0,-cost);
}
bool spfa(int s,int t,int n)
{
int u,v;
queue<int>q;
mem(vis,false);
mem(pre,-1);
for(int i=1; i<=n; i++) dis[i]=inf;
vis[s]=true;
dis[s]=0;
q.push(s);
while(!q.empty())
{
u=q.front();
q.pop();
vis[u]=false;
for(int i=first[u]; i!=-1; i=E[i].next)
{
v=E[i].v;
if(E[i].cap>E[i].flow&&dis[v]>dis[u]+E[i].cost)
{
dis[v]=dis[u]+E[i].cost;
pre[v]=i;
if(!vis[v])
{
q.push(v);
vis[v]=true;
}
}
}
}
if(dis[t]==inf)
return false;
return true;
}
int MCMF(int s,int t,int n)//minCostMaxFlow
{
int d;
int mincost=0;//maxflow当前最大流量,mincost当前最小费用
while(spfa(s,t,n))//表示找到了从s到t的最小费用路
{
d=inf;
for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //遍历反向边
d=min(d,E[i].cap-E[i].flow);
maxflow+=d;//更新最大流
mincost+=dis[t]*d;//dis[t]为该路径上单位流量费用之和
for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //增广路上正向边流量+d,反向边流量-d
{
E[i].flow+=d;
E[i^1].flow-=d;
}
}
return mincost;
}
char s[N][N];
struct node
{
int x,y;
} e1[N],e2[N];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)&&(n||m))
{
int l1=0,l2=0;
init();
for(int i=0;i<n;i++)
{
scanf("%s",s[i]);
for(int j=0;j<m;j++)
{
if(s[i][j]=='m')
{
e1[++l1].x=i;
e1[l1].y=j;
}
if(s[i][j]=='H')
{
e2[++l2].x=i;
e2[l2].y=j;
}
}
}
for(int i=1;i<=l1;i++) add(0,i,1,0);
for(int i=1;i<=l2;i++) add(l1+i,l1+l2+1,1,0);
for(int i=1;i<=l1;i++)
{
for(int j=1;j<=l2;j++)
{
int line=abs(e1[i].x-e2[j
].x)+abs(e1[i].y-e2[j].y);
add(i,l1+j,1,line);
}
}
printf("%d\n",MCMF(0,l1+l2+1,l1+l2+2));
}
}