Going Home
Time limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3299 Accepted Submission (s): 1674
Problem Descriptionon A grid map There is n little men and N houses. In each unit time, every little mans can move one unit step, either horizontally, or vertically, to a adjacent point. For each little mans, you need to pay a $ fee for every step he moves, until he enters a house. The task is complicated with the restriction, 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 Diffe Rent 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 ' indica TES There is a little man on this point.
You can think of all the the grid map as a quite large square, so it can hold n little men at the same time; Also, it is the okay if a little man steps on a grid with a house without entering this house.
Inputthere is one or more test cases in the input. Each case starts with a line giving-integers n and m, where N is the number of rows of the map, and M are the number of Columns. The rest of the input would be N lines describing the map. Assume both N and M are between 2 and inclusive. There would be the same number of ' H ' s and ' M's on the map; And there'll is at the most houses. Input would terminate with 0 0 for N and M.
Outputfor each test case, output one line with the single integer, which are the minimum amount, in dollars, your need to PA Y.
Sample Input2 2.mh.5 5HH.. m...............mm. H7 8...H ..... H....... H....mmmhmmmm ... H....... H....... H.... 0 0
Sample Output21028
Sourcepacific Northwest 2004 Topic meaning: Give a n*m map, ' m ' means people, ' H ' means the house, people each can go up and down to the neighboring units, each walk once the total cost of +1, each house can only accommodate one person, How much is the total cost when everyone walks to the house. Idea: People put left, the house put on the right, the Benquan value between people x[i] go to the house y[j] cost. Weights into negative numbers, km find the best match, the answer is negative. Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5#include <vector>6#include <queue>7#include <cmath>8#include <Set>9 using namespacestd;Ten One #defineN 105 A #defineINF 999999999 - - intMaxintXintY) {returnX>y?x:y;} the intMinintXintY) {returnX<y?x:y;} - intAbsintXintY) {returnx<0?-x:x;} - - structKM { + intN, M; - intG[n][n]; + intLx[n], ly[n], slack[n]; A intLeft[n], right[n]; at BOOLS[n], t[n]; - - voidInitintNintm) { - This->n =N; - This->m =m; -Memset (G,0,sizeof(g)); in } - to voidAdd_edge (intUintVintval) { +G[U][V] + =Val; - } the * BOOLDfsinti) { $S[i] =true; Panax Notoginseng for(intj =0; J < M; J + +) { - if(T[j])Continue; the intTMP = Lx[i] + ly[j]-G[i][j]; + if(!tmp) { AT[J] =true; the if(Left[j] = =-1||DFS (Left[j])) { +LEFT[J] =i; -Right[i] =J; $ return true; $ } -}ElseSLACK[J] =min (slack[j], TMP); - } the return false; - } Wuyi the voidUpdate () { - intA =inf; Wu for(inti =0; I < m; i++) - if(! T[i]) A =min (A, slack[i]); About for(inti =0; I < n; i++) $ if(S[i]) Lx[i]-=A; - for(inti =0; I < m; i++) - if(T[i]) Ly[i] + =A; - } A + intkm () { theMemset (left,-1,sizeof(left)); -Memset (right,-1,sizeof(right)); $memset (Ly,0,sizeof(Ly)); the for(inti =0; I < n; i++) { theLx[i] =-inf; the for(intj =0; J < M; J + +) theLx[i] =Max (Lx[i], g[i][j]); - } in for(inti =0; I < n; i++) { the for(intj =0; J < M; J + +) Slack[j] =inf; the while(1) { Aboutmemset (S),false,sizeof(S)); thememset (T,false,sizeof(T)); the if(Dfs (i)) Break; the Elseupdate (); + } - } the intAns =0; Bayi for(inti =0; I < n; i++) { theAns + =G[i][right[i]]; the } - returnans; - } the }kmm; the the intN, M; the CharMap[n][n]; - intG[n][n]; the the structnode{ the intx, y;94 node () {} theNodeintAintb) { thex=A; they=b;98 } About }a[n], b[n]; - 101 Main ()102 {103 intI, J, K;104 intNX, NY; the while(SCANF ("%d%d", &n,&m) = =2){106 if(n==0&&m==0) Break;107 108 for(i=0; i<n;i++) scanf ("%s", Map[i]);109nx=ny=0; theMemset (G,0,sizeof(g));111 for(i=0; i<n;i++){ the for(j=0; j<m;j++){113 if(map[i][j]=='m') a[nx++]=node (i,j); the if(map[i][j]=='H') b[ny++]=node (i,j); the } the }117 Kmm.init (nx,ny);118 for(i=0; i<nx;i++){119 for(j=0; j<ny;j++){ -Kmm.add_edge (i,j,-(ABS (a[i].x-b[j].x) +abs (a[i].y-( b[j].y) ));121 }122 }123printf"%d\n",-kmm.km ());124 } the}
HDU 1533 miles algorithm (best match with minimum weight)