# Niuke OJ: Symmetric binary tree

2023-01-23   ES

B – Going Home

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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```

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 , P ;
char str  ;
int Dis, Sink, Link , VIS , Best ;
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;
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  [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)
{{
dis = min (diss, root [pre] [x] .flow);
x = Pre:
}
x = sink;
While (x! = 0)
{{
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 (VIS, 0, SIZEOF (VIS));
MEMSET (Best, 2, SIZEOF (Best));
Best  = 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;
if (vis [i] == 0)
{{
vis [i] = 1;
q.push (i);
}
}
}
vis [now] = 0;
}
if (best [sink] <= 1000000)
Return 1;
Return 0;
}``````

source