hdu 1533 Going Home【KM匹配】

来源:互联网 发布:解压加密文件软件 编辑:程序博客网 时间:2024/04/29 19:31

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

 

题目大意:给你一个n*m的矩阵,其中H表示一个人,m表示一个房子,让所有人都进到房子里去,而且每个房子只能住一个人,每个人移动一个格子需要花费1个距离,问最小距离花费分配方案。

 

思路:

其费用流解法:http://blog.csdn.net/mengxiang000000/article/details/52166353


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();    }}



0 0