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 && 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 ...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

HDU 1533 Going Home (KM)

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

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 1533Going Home(费用流)

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

hdu 1533 Going Home(最小权值之和or最小费用流)

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

Going Home hdu 1533 最小费用最大流,费用流好用模板

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

HDU 1533 && POJ 2195 Going Home 最小费用流模板题

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

网络流 ( 最小费用最大流 )——Going Home ( HDU 1533 )

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1533 分析: 给出一个网格图,H代表房子,m代表人,两者数量相等,人每移动一格花费1$,求所有人...
  • FeBr2
  • FeBr2
  • 2016-07-27 21:33
  • 346
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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