Going home
Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2829 accepted submission (s): 1423
Problem descriptionon 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.
Inputthere 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.
Outputfor each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
Sample input2 2. MH. 5 5hh .. M ............... mm .. H 7 8... H ....... H ....... H .... mmmhmmmm... H ....... H ....... H .... 0 0
Sample output210
# Include <stdio. h> # include <string. h> # include <math. h> # include <stdlib. h> # define n 110 # define INF 99999999int n, m; char map [N] [N]; // store the int ma [N] [N] of the original Character Map; // an edge-like table can be matched to store int lx [N], Ly [N]; int vtx [N], vty [N]; int match [N]; int slack [N]; int CNT; int max (int A, int B) {return A> B? A: B;} int min (int A, int B) {return A> B? B: A;} int Hungary (INT dd) // Hungarian algorithm {int I; vtx [DD] = 1; for (I = 0; I <CNT; I ++) {If (vty [I]) continue; else {If (LX [DD] + ly [I] = ma [DD] [I]) {vty [I] = 1; if (Match [I] =-1 | Hungary (Match [I]) {match [I] = dd; return 1 ;}} else slack [I] = min (Slack [I], LX [DD] + ly [I]-ma [DD] [I]);} return 0;} void km_match () // maximum weight match {int I, j; int temp; memset (LX, 0, sizeof (LX); memset (ly, 0, sizeof (ly); for (I = 0; I <CN T; I ++) {for (j = 0; j <CNT; j ++) {lx [I] = max (LX [I], ma [I] [J]) ;}// indicates the current number of I people, the maximum distance to a house} for (I = 0; I <CNT; I ++) {for (j = 0; j <CNT; j ++) {slack [J] = inf; // initial infinity} while (1) {memset (vtx, 0, sizeof (vtx); memset (vty, 0, sizeof (vty); If (Hungary (I) // break of the Hungarian algorithm; else {temp = inf; For (j = 0; j <CNT; j ++) {If (! Vty [J]) {temp = min (temp, slack [J]) ;}}for (j = 0; j <CNT; j ++) {If (vtx [J]) lx [J]-= temp; If (vty [J]) ly [J] + = temp; else slack [J]-= temp ;}}} int main () {int I, J, K, ll; int CI, CJ; int sum; while (scanf ("% d", & N, & M) & n! = 0 & M! = 0) {memset (match,-1, sizeof (MATCH); // match array initial-1, record parent node CNT = 0; for (I = 0; I <n; I ++) {scanf ("% * C"); // get a carriage return for each line (j = 0; j <m; j ++) {scanf ("% C", & map [I] [J]); If (Map [I] [J] = 'M ') // if the number of individuals is {CNT ++; // Number of people recorded, You Need To }}/// search for the first two layers of traversal map in the first layer of four layers of Loops within M and find H ci = 0; cj = 0; for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (Map [I] [J] = 'M') // find a person {// traverse the map and find h for (k = 0; k <N; k ++) {for (LL = 0; ll <m; ll ++) {If (Map [k] [ll] = 'H ') {ma [CI] [CJ ++] = 100-(ABS (k-I) + ABS (LL-j )); // subtract edges for large numbers }}ci ++; // switch to the next row for storage Cj = 0; // CJ pointer back to 0} km_match (); // maximum weight match sum = 0; for (I = 0; I <CNT; I ++) {sum + = ma [Match [I] [I];} printf ("% d \ n", 100 * CNT-sum);} return 0 ;}
28
Source Pacific Northwest 2004