POJ 2195 Going Home(二分图最小匹配,KM算法)

题目链接:http://poj.org/problem?id=2195

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

题目大意:

给定n*m的矩阵,H代表房子,m代表人,每个人分配一个房子(数据保证人和房子数相等),花费为人走到这个房子的距离,可以往四个方向走,每一步花费1。求最小花费。

题目思路:

将人和房子分在两边,边的权值为对应距离,则是求二分图权值最小匹配,将比边权取负,答案再取反即可。

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 105;

int nx,ny;    //两边的点数
int g[N][N];  //二分图描述,g[i][j]代表i到j之间的权值,最大匹配初始化为-INF表示无边,同理最小匹配初始化为INF
int linker[N]; //记录y链接的哪个x,-1代表无
int lx[N],ly[N];//y中各点匹配状态,x,y中的点标号
int slack[N];        //slack为修改量
bool visx[N],visy[N];

bool DFS(int x)
{
    visx[x] = true;
    for(int y = 0; y < ny; y++)
    {
        if(visy[y])continue;     //用过了就继续
        int tmp = lx[x] + ly[y] - g[x][y];
        if(tmp == 0)   //符合匹配要求
        {
            visy[y] = true;
            if(linker[y] == -1 || DFS(linker[y]))
            {
                linker[y] = x;
                return true;
            }
        }
        else if(slack[y] > tmp)
            slack[y] = tmp;
    }
    return false;
}
int KM()
{
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));      //初始右边期望值都是0
    for(int i = 0;i < nx;i++)
    {
        lx[i] = -INF;             //左边期望值为最大边权
        for(int j = 0;j < ny;j++)
            if(g[i][j] > lx[i])
                lx[i] = g[i][j];
    }

    //开始解决左边匹配问题
    for(int x = 0;x < nx;x++)
    {
        for(int i = 0;i < ny;i++)
            slack[i] = INF;        //因为取最小值,初始化为无穷大
        while(true)
        {
            // 为左边解决归宿问题的方法是 :如果找不到就降低期望值,直到找到为止

            // 记录每轮匹配中左右两边是否被尝试匹配过
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(DFS(x))    //找到匹配,退出
                break;
            //没找到,降低期望值
            //最小可降低期望值
            int d = INF;
            for(int i = 0;i < ny;i++)
                if(!visy[i] && d > slack[i])
                    d = slack[i];
            for(int i = 0;i < nx;i++)
                if(visx[i])
                    lx[i] -= d;
            for(int i = 0;i < ny;i++)
            {
                if(visy[i])ly[i] += d;
                else slack[i] -= d;
            }
        }
    }
    int res = 0;
    for(int i = 0;i < ny;i++)
        if(linker[i] != -1)
            res += g[linker[i]][i];
    return res;
}


struct Node
{
    int x,y;
}peo[N],house[N];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
            break;
        char c;
        nx=0,ny=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf(" %c",&c);
                if(c=='m')
                {
                    peo[nx].x=i;
                    peo[nx].y=j;
                    nx++;
                }
                if(c=='H')
                {
                    house[ny].x=i;
                    house[ny].y=j;
                    ny++;
                }
            }
        }
        for(int i=0;i<nx;i++)
        {
            for(int j=0;j<ny;j++)
            {
                g[i][j] = -(abs(peo[i].x-house[j].x)+abs(peo[i].y-house[j].y));
            }
        }
        printf("%d\n",-KM());
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值