Going Home


Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3650    Accepted Submission(s): 1876


Problem 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. 


HDU 1533 && poj 2195 Going Home KM算法_#include


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


<pre name="code" class="cpp">//km算法第二题
//这道题之前也用费用流做过,现在用km算法再做一遍,效率比费用流好很多
//题意就是图上同等个数的m和H,让每个m走到H里,每步花费1,求最小的花费
//求二分图完美匹配的最小权值,那么把权值变负值,求出最大权值,然后再变负值就是最小权值
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;

const int N = 120;
const int INF = 0x3f3f3f3f;
char mpa[N][N];
int s[N][N];
int lx[N], ly[N], match[N], slack[N]; //lx,ly为顶标,nx,ny分别为x点集y点集的个数
bool visx[N], visy[N];
int nx, ny;

struct node
{
    int x, y;
}s1[N], s2[N];

bool hungary(int v)
{
    visx[v] = true;
    for(int i = 0; i < ny; i++)
    {
        if(visy[i]) continue;
        if(lx[v] + ly[i] == s[v][i])
        {
            visy[i] = true;
            if(match[i] == -1 || hungary(match[i]))
            {
                match[i] = v;
                return true;
            }
        }
        else slack[i] = min(slack[i], lx[v] + ly[i] - s[v][i]); //不在相等子图中slack 取最小的
    }
    return false;
}

void km()
{
    memset(ly, 0, sizeof ly);
    for(int i = 0; i < nx; i++)
        lx[i] = -INF;
    for(int i = 0; i < nx; i++)
        for(int j = 0; j < ny; j++)
            lx[i] = max(lx[i], s[i][j]); //lx初始化为与它关联边中最大的
    for(int i = 0; i < nx; i++)
    {
        memset(slack, 0x3f, sizeof slack);
        while(true)
        {
            memset(visx, 0, sizeof visx);
            memset(visy, 0, sizeof visy);

            if(hungary(i)) break; //若成功(找到了增广轨),则该点增广完成,进入下一个点的增广
            else //若失败(没有找到增广轨),则需要改变一些点的标号,使得图中可行边的数量增加。
            {
                int tmp = INF;
                for(int j = 0; j < ny; j++)
                    if(!visy[j])
                        tmp = min(tmp, slack[j]);
                for(int j = 0; j < nx; j++) //将所有在增广轨中(就是在增广过程中遍历到)的X方点的标号全部减去一个常数d,
                    if(visx[j]) lx[j] -= tmp;
                for(int j = 0; j < ny; j++) //所有在增广轨中的Y方点的标号全部加上一个常数d,修改顶标后,要把所有不在交错树中的Y顶点的slack值都减去d
                    if(visy[j]) ly[j] += tmp;
                    else slack[j] += tmp;
            }
        }
    }
}

int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m), n || m)
    {
        for(int i = 0; i < n; i++)
            scanf(" %s", mpa[i]);

        int cnt1 = 0, cnt2 = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(mpa[i][j] == 'm')
                    s1[cnt1].x = i, s1[cnt1].y = j, cnt1++;
                else if(mpa[i][j] == 'H')
                    s2[cnt2].x = i, s2[cnt2].y = j, cnt2++;

        for(int i = 0; i < cnt1; i++)
            for(int j = 0; j < cnt2; j++)
                s[i][j] = -abs(s1[i].x - s2[j].x) - abs(s1[i].y - s2[j].y);
        memset(match, -1, sizeof match);
        nx = cnt1, ny = cnt2;
        km();

        int res = 0;
        for(int i = 0; i < cnt2; i++)
            res += s[match[i]][i];
        printf("%d\n", -res);
    }

    return 0;
}