Description

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

Output

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

Question Overview: There are N people and N buildings on a map (H means a house M represents a person) now to allocate a person’s house. How can I allocate it?

Make everyone walking home is the shortest.

minimum cost maximum flow: each side not only has a traffic, but also has a unit fee. How can I consume the minimum expenses on the premise of the maximum flow?

5 5 网络 网络 网络 5 (u, v) deposits the unit fee of the current road, the corresponding reverse edge (V, U) deposits the negative value of the current road unit cost

The algorithm of this question is very similar to EK! It is improved on the basis of EK. The core of the algorithm: convert the cost of the edge to the length of the edge, find the starting point

When you reach the key point, you will meet this increase.

8 8: Initialize the source point to the side with a traffic of everyone, all houses to the side of the exchange point with a flow of 1, and then build people to the edge of the house. These edges flow

quantityis 1, the distance is the distance between people and houses

```
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
using namespace std;
TypeDef Struct
{{
int Flow;
int Cost;
} Edge;
TypeDef Struct
{{
int x;
int y;
} Point;
Point H [105], P [105];
Edge Road [505] [505];
char str [105] [105];
int Dis, Sink, Link [505], VIS [505], Best [505];
int spfa ();
Int main (void)
{{
Int n, m, i, j, x, a, b, pre, aNS;
While (scanf ("%d%d", & n, & m), n! = 0 || m! = 0)
{{
a = b = 0;
MEMSET (Road, 0, SIZEOF (Road));
for (i = 1; i <= n; i ++)
{{
for (j = 1; j <= m; j ++)
{{
scanf (" %c", & strong [i] [j];
if (str [i] [j] == 'h')
h [++ b] .x = i, h [b] .y = j;
if (str [i] [j] == 'm')
p [++ a] .x = i, p [a] .y = j;
}
}
n = a, m = b;
for (i = 1; i <= n; i ++)
{{
for (j = 1; j <= m; j ++)
{{
dis = abs (h [j] .x-p [i] .x)+abs (h [j] .y-p [i] .y);
Road [i] [n+j] .flow = 1, root [i] [n+j] .cost = diss, root [n+j] [i] .cost = -dis;
}
}
sink = n+m+1;
for (i = 1; i <= n; i ++)
Road [0] [i] .flow = 1;
for (i = 1; i <= m; i ++)
Road [n+i] [sink] .flow = 1;
Ans = 0;
While (spfa ()))
{{
x = sink;
DIS = 10000000;
While (x! = 0)
{{
pre = link [x];
dis = min (diss, root [pre] [x] .flow);
x = Pre:
}
x = sink;
While (x! = 0)
{{
pre = link [x];
Road [Pre] [x] .flow- = DIS;
Road [x] [pre] .flow += DIS;
aNS += Road [pre] [x] .cost*dis;
x = Pre:
}
}
Printf ("%d \ n", aNS);
}
Return 0;
}
int spfa ()
{{
int i, now;
Memset (link, -1, sizeof (link);
MEMSET (VIS, 0, SIZEOF (VIS));
MEMSET (Best, 2, SIZEOF (Best));
Best [0] = 0;
queue <int> q;
q.push (0);
While (q.empty () == 0) /*is different from EK. It is not only necessary to find a wide range of ways, but this increase is the shortest circuit! */
{{
Now = q.front ();
q.pop ();
for (i = 1; i <= sink; i ++)
{{
if (root [now] [i] .flow> 0 && best [i]> best [now]+root [now] [i] .cost)
{{
best [i] = best [now]+Road [now] [i] .cost;
link [i] = now;
if (vis [i] == 0)
{{
vis [i] = 1;
q.push (i);
}
}
}
vis [now] = 0;
}
if (best [sink] <= 1000000)
Return 1;
Return 0;
}
```