1013. Going Home
Constraints
Time Limit: 10 secs, Memory Limit: 32 MB
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 file e.in. 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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
Sample Output
2
10
28
// 此题考查KM最小匹配算法 套最小匹配模板就好
#include <iostream> #include <cstring> #include <cstdio> #include <string> #include <cmath> #define Mx 101; using namespace std; int n, result, d; int w[101][101], kmx[101] ,kmy[101]; bool _usex[101], _usey[101]; int linky[101]; int count_homes = 0; int count_members = 0; bool find( int i ) { _usex[i] = true; for( int j = 1;j <= n;j++ ) { if( _usey[j] ) continue; int t = kmx[i] + kmy[j] - w[i][j]; if( t == 0 ) { _usey[j] = true; if( !linky[j] || find( linky[j] ) ) { linky[j] = i; return true; } } else if( d > t ) d = t; } return false; } void KM() { for( int i = 1;i <= n;i++ ) { for( ; ; ) { memset( _usex,false,sizeof(_usex) ); memset( _usey,false,sizeof(_usey) ); d = Mx; if( find( i ) ) break; for( int j = 1;j <= n;j++ ) { if( _usex[j] ) kmx[j] -= d; if( _usey[j] ) kmy[j] += d; } } } } struct homes { int hx; int hy; } homes[1000]; struct members { int mx; int my; } members[1000]; int main() { int count_homes = 0; int count_members = 0; char map[1000][1000]; int m1, m2; while( scanf("%d%d",&m1, &m2) && (0 != m1) && (0 != m2)) { count_homes = count_members = 0; result = 0; memset( linky,0,sizeof( linky ) ); memset( kmx,0,sizeof( kmx ) ); memset( kmy,0,sizeof( kmy ) ); for (int i = 0; i < m1; i++) { for (int j = 0; j < m2; j++) { cin >> map[i][j]; if (map[i][j] == 'H') { homes[count_homes].hx = i; homes[count_homes].hy = j; count_homes++; } if (map[i][j] == 'm') { members[count_members].mx = i; members[count_members].my = j; count_members++; } } } int len = count_homes; n = len; for( int i = 1;i <= len;i++ ) for( int j = 1;j <= len;j++ ) { int road = 0; road = abs(homes[j-1].hx - members[i-1].mx) + abs(homes[j-1].hy - members[i-1].my); // 最小匹配 w[i][j] = -road; if( kmx[i] < w[i][j] ) kmx[i] = w[i][j]; } KM(); for( int i = 1;i <= len; i++ ) result += ( kmx[i] + kmy[i] ); // 最小匹配 printf( "%d\n",-result ); } return 0; }