POJ 2195【KM 二分图带权最大匹配】

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


有若干个人和若干个房子在一个给定网格中,每人走一个都要一定花费,每个房子只能容纳一人,现要求让所有人进入房子,且总花费最小。

KM–二分图带权匹配

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
namespace KM {
    const int qwq = 0x7fffffff;
    int w[1000][1000];  //w数组记录边权值
    int line[1000], usex[1000], usey[1000], cx[1000], cy[1000];
    //line数组记录右边端点所连的左端点, usex,usey数组记录是否曾访问过,也是判断是否在增广路上,cx,cy数组就是记录点的顶标
    int N, ans, M;  //n左m右
    bool find(int x) {
        usex[x] = 1;
        for (int i = 1; i <= M; i++) {
            if ((usey[i] == 0) && (cx[x] + cy[i] == w[x][i])) {   //如果这个点未访问过并且它是子图里面的边
                usey[i] = 1;
                if ((line[i] == 0) || find(line[i])) {   //如果这个点未匹配或者匹配点能更改
                    line[i] = x;
                    return true;
                }
            }
        }
        return false;
    }
    int km() {
        for (int i = 1; i <= N; i++) {  //分别对左边点依次匹配
            while (true) {
                int d = qwq;
                memset(usex, 0, sizeof(usex));
                memset(usey, 0, sizeof(usey));
                if (find(i)) break;  //直到成功匹配才换下一个点匹配
                for (int j = 1; j <= N; j++) {
                    if (usex[j]) {
                        for (int k = 1; k <= M; k++)
                            if (!usey[k]) d = min(d, cx[j] + cy[k] - w[j][k]);  //计算d值
                    }
                }
                if (d == qwq) return -1;
                for (int j = 1; j <= N; j++)
                    if (usex[j]) cx[j] -= d;
                for (int j = 1; j <= M; j++)
                    if (usey[j]) cy[j] += d;     //添加新边
            }
        }
        ans = 0;
        for (int i = 1; i <= M; i++)
            ans += w[line[i]][i];
        return ans;
    }
    void init() {
        memset(cy, 0, sizeof(cy));
        memset(w, 0, sizeof(w));
        memset(cx, 0, sizeof(cx));
        memset(line, 0, sizeof(line));
    }
}
struct node
{
    int x, y;
    node(int a, int b) { x = a; y = b; }
    int dis(node d) {
        return abs(x - d.x) + abs(y - d.y);
    }
};
char mp[111][111];
int n, m;
vector<node>vh, vm;

int main()
{
    while (~scanf("%d%d", &n, &m), n&&m)
    {
        KM::init();
        vh.clear();
        vm.clear();
        for (int i = 0; i < n; i++)
        {
            scanf("%s", mp[i]);
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (mp[i][j] == 'H')
                    vh.push_back(node( i,j ));
                if (mp[i][j] == 'm')
                    vm.push_back(node( i,j ));
            }
        }
        KM::N = vh.size();
        KM::M = vm.size();
        for (int i = 0; i < vh.size(); i++)
        {
            int maxd = 0x80000000;
            for (int j = 0; j < vm.size(); j++)
            {
                KM::w[i+1][j+1] = (vh[i].dis(vm[j]));
                maxd = max(KM::w[i+1][j+1], maxd);
                KM::w[i+1][j+1] *= -1;
            }
            KM::cx[i+1] = maxd;
        }
        printf("%d\n", -1*KM::km());
    }
}

猜你喜欢

转载自blog.csdn.net/jinmingyi1998/article/details/81240362