# Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4350    Accepted Submission(s): 2260

Problem 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

Source

Pacific Northwest 2004

1、经典模型的KM匹配，要求的是最小匹配。

2、建图：将h和m的每个点的信息保存起来，然后求每个h到每个m的曼哈顿距离求出来保存到map【i】【j】中，表示第i个h到第j个m的距离，然后得到一个map【】【】矩阵。

3、因为是要求最小匹配，那么我们用一个极大值：100*100（足够了）-map【i】【j】得到一个新的map【i】【j】，比如原来map【i】【j】=9999，现在map【i】【j】=1；较大值变成了较小值，较小值变成了较大值。那么现在匹配到的最大匹配值就是在原图中的最小匹配。那么ans=conth*100*100-最大匹配；

Ac代码：

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
int x,y;
}h[105*105],m[105*105];
char a[105][105];
int map[105][105];
int lx[105*105];
int ly[105*105];
int vx[105*105];
int vy[105*105];
int match[105*105];
int low;
int n,mm;
int conth,contm;
int abs(int aaa)
{
if(aaa<0)return -aaa;
else return aaa;
}
void getmap()
{
memset(map,0,sizeof(map));
for(int i=0;i<conth;i++)
{
for(int j=0;j<contm;j++)
{
map[i][j]=abs(h[i].x-m[j].x)+abs(h[i].y-m[j].y);
}
}
}
int find(int u)
{
vx[u]=1;
for(int i=0;i<contm;i++)
{
if(vy[i]==1)continue;
int tmp=lx[u]+ly[i]-map[u][i];
if(tmp==0)
{
vy[i]=1;
if(match[i]==-1||find(match[i]))
{
match[i]=u;
return 1;
}
}
else if(tmp<low)low=tmp;
}
return 0;
}
void KM()
{
memset(match,-1,sizeof(match));
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
for(int i=0;i<conth;i++)
{
for(int j=0;j<contm;j++)
{
map[i][j]=100*100-map[i][j];
}
}
for(int i=0;i<conth;i++)
{
for(int j=0;j<contm;j++)
{
lx[i]=max(lx[i],map[i][j]);
}
}
for(int i=0;i<conth;i++)
{
while(1)
{
low=0x3f3f3f3f;
memset(vx,0,sizeof(vx));
memset(vy,0,sizeof(vy));
if(find(i))break;
for(int j=0;j<conth;j++)
{
if(vx[j])lx[j]-=low;
}
for(int j=0;j<contm;j++)
{
if(vy[j])ly[j]+=low;
}
}
}
int sum=0;
for(int i=0;i<conth;i++)
{
sum+=map[match[i]][i];
}
printf("%d\n",conth*100*100-sum);
}
int main()
{
while(~scanf("%d%d",&n,&mm))
{
if(n==0&&mm==0)break;
conth=0;
contm=0;
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
for(int j=0;j<mm;j++)
{
if(a[i][j]=='H')
{
h[conth].x=i;
h[conth++].y=j;
}
else if(a[i][j]=='m')
{
m[contm].x=i;
m[contm++].y=j;
}
}
}
getmap();
KM();
}
}


• 本文已收录于以下专栏：

## Going Home

• lzh823046544
• 2016年09月20日 16:25
• 125

## 【POJ 2195】 Going Home（最小费）

【POJ 2195】 Going Home（最小费） Going Home Time Limit: 1000MS   Memory Limit:...
• ChallengerRumble
• 2015年11月13日 14:40
• 1509

## Going Home

Going HomeTime Limit:1000MS  Memory Limit:65536KTotal Submit:19 Accepted:6 DescriptionOn a grid map ...
• abcjennifer
• 2010年08月27日 19:16
• 1738

## hdu acm 1533 Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submissi...
• hyf20144055065
• 2016年02月09日 14:41
• 365

## Going Home

• liujie619406439
• 2014年07月21日 12:27
• 842

## 【最小费用最大流】【HDU1533】【Going Home】

• zy691357966
• 2015年04月05日 19:23
• 1831

## HDU 1533 Going Home(最小费用流)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To...
• u010372095
• 2015年06月23日 22:33
• 912

## HDU 1533 — Going Home 最小费用最大流

• L__emon
• 2015年05月14日 18:36
• 377

## HDU 1533 Going Home（KM完美匹配）

HDU 1533 Going Home 题目链接 题意：就是一个H要对应一个m，使得总曼哈顿距离最小 思路：KM完美匹配，由于是要最小，所以边权建负数来处理即可 代码： ...
• u011217342
• 2014年10月30日 22:00
• 624

## HDU 1533 Going Home (KM)

Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To...
• qq_37025443
• 2017年08月04日 10:59
• 80

举报原因： 您举报文章：hdu 1533 Going Home【KM匹配】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)