HDU 1533 Going Home

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.

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

Solution

二分图最大权匹配模板,注意建模的时候权值为负,这样跑最大匹配后的答案取相反数就是答案了。

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <cstdio>
int n, m, th = 0, tm = 0;
const int maxn = 150;
const int INF = 1<<30;
int hh[maxn], hl[maxn], mh[maxn], ml[maxn];
int lx[maxn], ly[maxn], slack[maxn], left[maxn], w[maxn][maxn];
bool s[maxn], t[maxn];
char c;
int abs(int x) { if (x < 0) return -x; return x; }
int dis(int hid, int mid) { return abs(hh[hid]-mh[mid])+abs(hl[hid]-ml[mid]); }
char sbj() {
    char c;
    while (true) {
        c = getchar();
        if (c == '.' || c == 'H' || c == 'm') return c;
    }
}
void update() {
    int a = INF;
    for (int i = 1; i <= tm; ++i) if (!t[i] && a > slack[i]) a = slack[i];
    for (int i = 1; i <= tm; ++i) {
        if (t[i]) ly[i] += a;
        else slack[i] -= a;
    }
    for (int i = 1; i <= th; ++i)
        if (s[i]) lx[i] -= a;
}
bool match(int x) {
    if (x == -1) return false;
    s[x] = true;
    int tmp;
    for (int y = 1; y <= tm; ++y) {
        if (t[y]) continue;
        tmp = lx[x] + ly[y] - w[x][y];
        if (tmp == 0) {
            t[y] = true;
            if (left[y] == -1 || match(left[y])) {
                left[y] = x;
                return true;
            }
        }
        else { if (slack[y] > tmp) slack[y] = tmp; }
    }
    return false;
}
int km() {
    for (int i = 1; i <= th; ++i) {
        lx[i] = -INF;
        for (int j = 1; j <= tm; ++j) if (lx[i] < w[i][j]) lx[i] = w[i][j];
    }
    for (int i = 1; i <= tm; ++i) ly[i] = 0, left[i] = -1;
    
    for (int x = 1; x <= th; ++x) {
        for (int i = 1; i <= tm; ++i) slack[i] = INF;
        while (true) {
            for (int i = 1; i <= th; ++i) s[i] = false;
            for (int i = 1; i <= tm; ++i) t[i] = false;
            if (match(x)) break;
            update();
        }
    }
    int ret = 0;
    for (int i = 1; i <= tm; ++i)
        if (left[i] > 0)
            ret += w[left[i]][i];
    return -ret;
}
int main() {
    while (true) {
        scanf("%d", &n);
        scanf("%d", &m);
        if (n == 0 || m == 0) break;
        th = 0; tm = 0;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j) {
                c = sbj();
                if (c == 'H') {
                    hh[++th] = i;
                    hl[th] = j;
                }
                if (c == 'm') {
                    mh[++tm] = i;
                    ml[tm] = j;
                }
            }
        for (int i = 1; i <= th; ++i)
            for (int j = 1; j <= tm; ++j)
                w[i][j] = -dis(i,j);
        printf("%d\n", km());
    }
}