Language:
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 |
题意:有n*m的图,H表示房,m表示人,人要住房,但是每个房只能住一个人,每个人的花费是其到要住的房的距离,问所有人费用总和的最小值。
思路:最小权匹配。我们从人向所有房连边,权值为到该房的最短距离的负值,然后利用KM算法求出最大权匹配,(因为权值为负值,所以求的是实际的最小权匹
配),然后累加每个人选择对应房的距离,累加的和即为答案。详见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100+50;
const int inf=0x3fffffff;
int n,m,h_cnt,m_cnt;
bool S[MAXN],T[MAXN];
char str[MAXN];
int w[MAXN][MAXN],link[MAXN],lx[MAXN],ly[MAXN];
struct node
{
int x;
int y;
}H[MAXN],M[MAXN];
void init()
{
h_cnt=m_cnt=0;
memset(link,-1,sizeof(link));
}
int path(int u)
{
S[u]=1;
for(int v=1;v<=n;v++) if(lx[u]+ly[v]==w[u][v] && !T[v])
{
T[v]=1;
if(link[v]==-1 || path(link[v]))
{
link[v]=u;
return 1;
}
}
return 0;
}
void update()
{
int x=inf;
for(int i=1;i<=n;i++) if(S[i]){
for(int j=1;j<=n;j++) if(!T[j])
x=min(x,lx[i]+ly[j]-w[i][j]);
}
for(int i=1;i<=n;i++)
{
if(S[i]) lx[i]-=x;
if(T[i]) ly[i]+=x;
}
}
void KM()
{
n=m_cnt;
for(int i=1;i<=n;i++)
{
lx[i]=ly[i]=0;
for(int j=1;j<=n;j++)
lx[i]=max(lx[i],w[i][j]);
}
for(int i=1;i<=n;i++)
{
for(;;){
for(int j=1;j<=n;j++)
S[j]=T[j]=0;
if(path(i)) break;
else update();
}
}
}
int main()
{
//freopen("text.txt","r",stdin);
while(~scanf("%d%d",&n,&m) && n+m )
{
init();
for(int i=0;i<n;i++)
{
scanf("%s",str);
for(int j=0;j<m;j++)
{
if(str[j]=='H')
H[++h_cnt].x=i,H[h_cnt].y=j;
else if(str[j]=='m')
M[++m_cnt].x=i,M[m_cnt].y=j;
}
}
for(int i=1;i<=m_cnt;i++)
for(int j=1;j<=h_cnt;j++)
w[i][j]=-abs(M[i].x-H[j].x)-abs(M[i].y-H[j].y);
KM();
int ans=0;
for(int j=1;j<=n;j++)
{
int i=link[j];
ans+=abs(M[i].x-H[j].x)+abs(M[i].y-H[j].y);
}
printf("%d\n",ans);
}
return 0;
}