Poj 2195 going home (Maximum Weight Matching in a bipartite graph)

Source: Internet
Author: User
Going home
Time limit:1000 ms   Memory limit:65536 K
Total submissions:13981   Accepted:7156

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 done 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, '. '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, aggressive. 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. mh.5 5hh .. M ............... mm .. h7 8... H ....... H ....... H .... mmmhmmmm... H ....... H ....... H .... 0 0

Sample output

 
21028

Source

The maximum weight of the Pacific Northwest 2004 bipartite graph is matched. Used for the exercise template.
/*  Poj 2195 minimum weight matching g ++ 744 K 0 ms  */  # Include <Stdio. h> # Include <Iostream> # Include < String . H> # Include <Algorithm> # Include <Math. h> Using   Namespace  STD;  // *************************************  //  Max Weight Matching template (kmAlgorithm) O (M * m * n)  //  Returns the optimal matching value in the form of an adjacent matrix. The size of the input bipartite graph is M, N.  //  Adjacent matrix G, indicating the weight  //  Matcher and match2 return an optimal match. The unmatched vertex match value is-1.  //  Be sure to pay attention to m <= n otherwise the loop cannot be terminated  //  Minimum weight matching can take the opposite number of values  //  Initialization //  For (I = 0; I <maxn; I ++)  //  For (j = 0; j <maxn; j ++) g [I] [J] =-INF;  //  For existing edge G [I] [J] = val;  Const   Int Maxn = 120  ;  Const   Int INF = 0x3fffffff  ;  Int G [maxn] [maxn], mattings [maxn], match2 [maxn];  Int Km ( Int M, Int  N ){  Int  I, J, K, P, Q;  Int  L1 [maxn], L2 [maxn];  Int  S [maxn], t [maxn];  Int Ret = 0  ;  For (I =0 ; I <m; I ++ ) {L1 [I] =- INF;  For (J = 0 ; J <n; j ++ )  If (G [I] [J]> L1 [I]) L1 [I] = G [I] [J];  If (L1 [I] =-INF) Return - 1 ; // Unable to match  }  For (I = 0 ; I <n; I ++) L2 [I] = 0  ; Memset (mattings, - 1 , Sizeof  (Match2); memset (match2, - 1 , Sizeof  (Match2 ));  For (I = 0 ; I <m; I ++) {Memset (t, - 1 , Sizeof  (T ));  For (S [p = q = 0 ] = I; P <= Q & mattings [I] < 0 ; P ++ )  For (K = s [p], j = 0 ; J <n & mattings [I] < 0 ; J ++ )  If (L1 [k] + L2 [J] = G [k] [J] & T [J] < 0  ) {S [ ++ Q] = match2 [J], t [J] = K;  If (S [Q] < 0  )  For (P = J; P> = 0 ; J = P) match2 [J] = K = T [J], P = matebook [K], matebook [k] = J ;}  If (Mattings [I] < 0 ){  For (I --, P = inf, K = 0 ; K <= Q; k ++ )  For (J = 0 ; J <n; j ++ )  If (T [J] < 0 & L1 [s [k] + L2 [J]-G [s [k] [J] < P) P = L1 [s [k] + L2 [J]- G [s [k] [J];  For (J =0 ; J <n; j ++) If (T [J]> = 0 ) L2 [J] + = P;  For (K = 0 ; K <= Q; k ++) L1 [s [k]-= P ;}}  For (I = 0 ; I <m; I ++ ){  If (Mattings [I] < 0 ) Return -1 ; //  Unable to match          If (G [I] [mattings [I] <=-INF) Return - 1  ; RET + = G [I] [mate8 [I];}  Return  RET ;}  //  **************************************** *******  Struct  Node {  Int X, Y;}; node node1 [maxn], node2 [maxn];  Char  STR [maxn];  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int  N, m;  Int  NX, NY;  While (Scanf ( " % D  " , & N, & M )! = EOF ){  If (N = 0 & M = 0 ) Break  ; NX = 0  ; Ny = 0  ;  For ( Int I = 0 ; I <n; I ++ ) {Scanf (  "  % S  " ,& Str );  For ( Int J = 0 ; J <m; j ++ ){  If (STR [J] = '  M  '  ) {Node1 [NX]. x =I; node1 [NX]. Y = J; NX ++ ;}  Else   If (STR [J] = '  H  '  ) {Node2 [NY]. x = I; node2 [NY]. Y = J; ny ++ ;}}} For ( Int I = 0 ; I <NX; I ++ )  For ( Int J = 0 ; J <NY; j ++ ) {G [I] [J] =-ABS (node1 [I]. x-node2 [J]. X)-ABS (node1 [I]. Y- Node2 [J]. Y);} printf (  "  % D \ n  " ,- Km (NX, NY ));} Return   0  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.