Going Home

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
题意:给出人跟房子的位置,人跑到房子会有消耗(两点的距离)。每人只能进一个房子,人可以路过所有位置。
求所有人住进房子的最小消耗和。
分析:满足二分图,边权为人到房子的距离。
注意:最大权匹配要求两边的点一样多,如果哪边得点不够,得额外加点,这些点到另一边的权值都为0.

#include <stdio.h>
#define maxn 200
#define inf (1 << 29)
#define Min(a, b) a < b ? a : b
#define Max(a, b) a > b ? a : b
struct T
{
    
int v, next;
}fn[maxn 
* maxn];
struct P
{
    
int x, y;
}H[maxn], M[maxn];
char s[maxn];
int link[maxn], alink[maxn], lx[maxn], ly[maxn], sx[maxn], sy[maxn], g[maxn], map[maxn][maxn];
int n, m, slack;
int fsb(int a)
{
    
return a > 0 ? a : -a;
}
int dfs(int u)
{
    
int v, tmp;
    sx[u] 
= 1;
    
for (v = 0; v < m; v++)
    {
        
if (!sy[v])
        {
            tmp 
= lx[u] + ly[v] - map[u][v];
            
if (tmp == 0)
            {
                sy[v] 
= 1;
                
if (link[v] == -1 || dfs(link[v]))
                {
                    link[v] 
= u;
                    
return 1;
                }
            }
            
else
            {
                slack 
= Min(slack, tmp);
            }
        }
    }
    
return 0;
}
int KM()
{
    
int i, j, sum;
    
for (i = 0; i < n; i++)
    {
        
for (j = 0, lx[i] = 0; j < m; j++)
        {
            lx[i] 
= Max(lx[i], map[i][j]);
        }
    }
    
for (i = 0; i < m; i++)
    {
        ly[i] 
= 0;
        link[i] 
= -1;
    }
    
for (i = 0; i < n; i++)
    {
        
while (1)
        {
            
for (j = 0; j < n; j++)
            {
                sx[j] 
= 0;
            }
            
for (j = 0; j < m; j++)
            {
                sy[j] 
= 0;
            }
            slack 
= inf;
            
if (dfs(i))
            {
                
break;
            }
            
for (j = 0; j < n; j++)
            {
                
if (sx[j])
                {
                    lx[j] 
-= slack;
                }
            }
            
for (j = 0; j < m; j++)
            {
                
if (sy[j])
                {
                    ly[j] 
+= slack;
                }
            }
        }
    }
    sum 
= 0;
    
for (i = 0; i < m; i++)
    {
        
if (link[i] != -1)
        {
            sum 
+= map[link[i]][i];
        }
    }
    
return sum;
}
int main()
{
    
int i, j, num1, num2;
    
while (scanf("%d%d"&n, &m), n || m)
    {
        
for (i = 1, num1 = num2 = 0; i <= n; i++)
        {
            scanf(
"%s", s);
            
for (j = 0; s[j]; j++)
            {
                
if (s[j] == 'H')
                {
                    H[num1].x 
= i, H[num1].y = j, num1++;
                }
                
else if (s[j] == 'm')
                {
                    M[num2].x 
= i, M[num2].y = j, num2++
                }
            }
        }
        m 
= num1, n = num2;
        
for (i = 0; i < n; i++)
        {
            
for (j = 0; j < m; j++)
            {
                map[i][j] 
= -1 * (fsb(M[i].x - H[j].x) + fsb(M[i].y- H[j].y));
            }
        }
        printf(
"%d\n"-KM());
    }
    
return 0;
}