8.15 K - Going Home

                                                                    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美元。

求最小需要花费多少美元能让所有人都进入到房子中(每个人只能进入一间房子,每个房子只能容纳一个人)。


思路:将每个m点看成一个顶点并编号,记录它的位置(x1,y1),每个H点看成一个顶点并编号,记录位置( x2,y2 ),然后用每一个m点遍历每一个H点,矩阵存储他们的距离(|(x1-x2)|+|(y1-y2)|)的相反数,这个距离的绝对值等于从当前m点到H点的花费,储存成负数求出最大权值的绝对值是最下的, KM 求最小权,跟最大权一种套路,只是建图的时候权值取负,这样求出的最大权绝对值是最小的。取反回来后,是所求的解。
#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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值