poj2195 二分图的最佳匹配

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
/**
poj 2195 二分图的最佳匹配
题目大意:每一个人到每一房间的很重坐标之和为其到该房间的花费,一个房间只能容下一个人,问如何安排能
         使所有的人都有房安排的前提下总体花费最小。
解题思路:人和边之间两两建边,利用KM算法求取答案即可。

*/
/*************************************************************************
**************************************************************************
KM算法模板C++
作用:
     求二分图的最佳匹配
注意:
      (1)for (i:1~n)for (j:1~n)scanf (w[i][j]);
          w[i][j],表示左边第i点匹配右边第j点的价值。i,j:从1开始。
          主函数调用:ans=KM(); ans的值即为所求。
      (2)所求为最大完备匹配,若是求最小,则把边的权值取相反数,跑一遍模板,
          最后结果再取相反数即可。
**************************************************************************
*************************************************************************/
#include <stdio.h>
#include <string.h>
#define M 310
#define inf 0x3f3f3f3f

int abs(int x)
{
    return x>0?x:-x;
}
int n,m,nx,ny;
int link[M],lx[M],ly[M],slack[M];///lx,ly为顶标,nx,ny分别为x点集y点集的个数
int visx[M],visy[M],w[M][M];

int DFS(int x)
{
    visx[x] = 1;
    for (int y = 1; y <= ny; y ++)
    {
        if (visy[y]) continue;
        int t = lx[x] + ly[y] - w[x][y];
        if (t == 0)
        {
            visy[y] = 1;
            if (link[y] == -1||DFS(link[y]))
            {
                link[y] = x;
                return 1;
            }
        }
        else if (slack[y] > t)  ///不在相等子图中slack 取最小的
            slack[y] = t;
    }
    return 0;
}
int KM()
{
    int i,j;
    memset (link,-1,sizeof(link));
    memset (ly,0,sizeof(ly));
    for (i = 1; i <= nx; i ++)          ///lx初始化为与它关联边中最大的
        for (j = 1,lx[i] = -inf; j <= ny; j ++)
            if (w[i][j] > lx[i])
                lx[i] = w[i][j];

    for (int x = 1; x <= nx; x ++)
    {
        for (i = 1; i <= ny; i ++)
            slack[i] = inf;
        while (1)
        {
            memset (visx,0,sizeof(visx));
            memset (visy,0,sizeof(visy));
            if (DFS(x))     ///若成功(找到了增广轨),则该点增广完成,进入下一个点的增广
                break;  ///若失败(没有找到增广轨),则需要改变一些点的标号,使得图中可行边的数量增加。
            ///方法为:将所有在增广轨中(就是在增广过程中遍历到)的X方点的标号全部减去一个常数d,
            ///所有在增广轨中的Y方点的标号全部加上一个常数d
            int d = inf;
            for (i = 1; i <= ny; i ++)
                if (!visy[i]&&d > slack[i])
                    d = slack[i];
            for (i = 1; i <= nx; i ++)
                if (visx[i])
                    lx[i] -= d;
            for (i = 1; i <= ny; i ++) ///修改顶标后,要把所有不在交错树中的Y顶点的slack值都减去d
                if (visy[i])
                    ly[i] += d;
                else
                    slack[i] -= d;
        }
    }
    int res = 0;
    for (i = 1; i <= ny; i ++)
        if (link[i] > -1)
            res += w[link[i]][i];
    return res;
}

char a[104][104];
struct note
{
    int x,y;
}node1[105],node2[105];

int main()
{
    while(~scanf("%d%d%*c",&n,&m))
    {
        if(n==0&&m==0)break;
        nx=0,ny=0;
        for(int i=0;i<n;i++)
        {
           scanf("%s",a[i]);
           for(int j=0;j<m;j++)
           {
               if(a[i][j]=='m')
               {
                   node1[++nx].x=i;
                   node1[nx].y=j;
               }
               else if(a[i][j]=='H')
               {
                   node2[++ny].x=i;
                   node2[ny].y=j;
               }
           }
        }
        for(int i=1;i<=nx;i++)
        {
            for(int j=1;j<=ny;j++)
            {
                w[i][j]=-abs(node1[i].x-node2[j].x)-abs(node1[i].y-node2[j].y);
                //printf("%d ",w[i][j]);
            }
           // printf("\n");
        }
        printf("%d\n",-KM());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值