hdu 1533 Going Home【KM匹配】

原创 2016年08月31日 13:54:52

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小白有写的不好不对的地方请指出~

相关文章推荐

HDU 1533 Going Home【km应用】

HDU 1533 Going Home【km应用】http://acm.hdu.edu.cn/showproblem.php?pid=1533

HDU 1533 Going Home (KM)

Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To...

HDU 1533 && poj 2195 Going Home KM算法

Description On a grid map there are n little men and n houses. In each unit time, every little man ...

HDU 1533 Going Home【最小费用流|KM算法】

N*M图里有x个人x个房间,让每个人进到任意一个房间,一个房间只能容纳一个人,可以经过房间的位置却不进入。每个人都会找个房间进入,求他们走的路程和最短 最小费用流,建图: 源点 对每个人建边 容量为1...

HDU_1533 Going Home(最优匹配) 解题报告

转载请注明出自cxb:http://blog.csdn.net/cxb569262726 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 ...

hdu 1533 Going Home 【最小权的二分图匹配】

Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

hdu 1533 Going Home(最小费用最大流)

题目链接: 点击打开链接 题目大意: 给出一张地图,给出人和屋子的位置,问每个人都有屋子住的走的最小的距离之和 题目分析: 首先屋子的数量和人的数量相等,那么每个人一定有屋子住,要求最小...

HDU 1533 Going Home

费用流

HDU_1533_Going Home(最小费用流模板)

题意:N*M的地图上有等数量的人和房子,每个房子只能容纳一个人,人可以上下左右移动,每移动一格需要花费1美元,问所有人和房子匹配好的最小费用是多少。 分析:最小费用流。关键在于建图。假设有n个人,首先...

hdu 1533 || poj 2195 Going Home (最小费用最大流)

题目链接:   poj 2195 题目大意:   给出NxM的地图,'.'表示可以走的,'H'表示家,'m'表示人,H和m的数目相同                   求把所有人移动到H的最小步数 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1533 Going Home【KM匹配】
举报原因:
原因补充:

(最多只允许输入30个字)