K - Going Home
On a grid map there are n little men and nhouses. In each unit time, every little man can move one unit step, eitherhorizontally, or vertically, to an adjacent point. For each little man, youneed 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 accommodateonly one little man.
Your task is to compute the minimum amount of money you need to pay in order tosend these n little men into those n different houses. The input is a map ofthe scenario, a '.' means an empty space, an 'H' represents a house on thatpoint, 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 canhold n little men at the same time; also, it is okay if a little man steps on agrid with a house without entering that house.
Input
There are one or more test cases in the input. Each casestarts with a line giving two integers N and M, where N is the number of rowsof the map, and M is the number of columns. The rest of the input will be Nlines 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 therewill 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 singleinteger, 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
题意:一个N行M列的矩阵,其中“.”代表空地,“H”代表房子,“m”代表人,其中有 n 个房子和 n 个人。现在要求每个人进入其中的一间房子,且每人每走一步需要支付1美元。
求最小需要花费多少美元能让所有人都进入到房子中(每个人只能进入一间房子,每个房子只能容纳一个人)。
#include<stdio.h>
#include<string.h>
int a[110][110],c[110],x1[110],x2[110],y1[110],y2[110],match[110];
char s[200][200];
int m,n,t1,t2;
int max(int x,int y)
{
if(x<y)
x=y;
return x;
}
int min(int x,int y)
{
if(x>y)
x=y;
return x;
}
int ads(int x)
{
if(x<0)
x=-x;
return x;
}
struct node{
int a1,b1;
}sa[110],sb[110]; //结构体存储m,H点坐标
int dfs(int w)
{
int i,t3;
x2[w]=1;
for(i=1;i<=t1;i++)
{
if(y2[i])
continue;
t3=x1[w]+y1[i]-a[w][i];
if(t3==0)
{
y2[i]=1;
if(match[i]==-1||dfs(match[i]))
{
match[i]=w;
return 1;
}
}
else
{
c[i]=min(c[i],t3);
}
}
return 0;
}
int km()//km算法
{
int i,j,t;
memset(match,-1,sizeof(match));
memset(y1,0,sizeof(y1));
/* for(int i=1;i<=105;i++)//初始化x1数组,但是用下边方法处理是可省略
{
x1[i]=-1e9;
}
*/ for(i=1;i<=t1;i++)
{
x1[i]=a[i][1];
for(j=2;j<=t1;j++)
{
x1[i]=max(x1[i],a[i][j]);
}
}
for(i=1;i<=t1;i++)
{
for(j=1;j<=t1;j++)
{
c[j]=1e9;
}
while(1)
{
memset(x2,0,sizeof(x2));
memset(y2,0,sizeof(y2));
if(dfs(i))
break;
t=1e9;
for(j=1;j<=t1;j++)
if(!y2[j]&&t>c[j])
{
t=c[j];
}
for(j=1;j<=t1;j++)
{
if(x2[j])
x1[j]-=t;
}
for(j=1;j<=t1;j++)
{
if(y2[j])
y1[j]+=t;
else
c[j]-=t;
}
}
}
int sum=0;
for(i=1;i<=t1;i++)
{
if(match[i]>-1)
sum+=a[match[i]][i];
}
return sum;
}
int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m),m+n!=0)
{
memset(a,0,sizeof(a));
memset(s,0,sizeof(s));
for(i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
}
t1=t2=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(s[i][j]=='m')//记录每个m点坐标
{
sa[++t1].a1=i;
sa[t1].b1=j;
}
if(s[i][j]=='H')//记录每一个H点坐标
{
sb[++t2].a1=i;
sb[t2].b1=j;
}
}
}
for(i=1;i<=t1;i++)
{
for(j=1;j<=t2;j++)
{
a[i][j]=ads(sa[i].a1-sb[j].a1)+ads(sa[i].b1-sb[j].b1);//计算每个m点和每个H点距离
a[i][j]=-a[i][j];
}
}
printf("%d\n",-km());
}
return 0;
}