Sicily 1013. Going Home

1013. Going Home

Constraints

Time Limit: 10 secs, Memory Limit: 32 MB

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 file e.in. Each case starts with a line giving two integersN 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算法,此处也就不献丑了,下面是在网上找到的模板,是求二分图最大匹配的,先将图中所有权值取相反数,那么最大值就是绝对值最小的值。

http://www.cnblogs.com/skyming/archive/2012/02/18/2356919.html

// Problem#: 1013
// Submission#: 3160682
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;

#define MAX_POINT 101  // 每个集合中的点数,可自己改
#define INF 0xffffff


int w[MAX_POINT][MAX_POINT];  // 边权值,可自己改
int lx[MAX_POINT], ly[MAX_POINT];  // 顶标
int linky[MAX_POINT];  // 存储求解过程
bool visx[MAX_POINT], visy[MAX_POINT];  // 记录两个集合中的点的访问情况
int slack[MAX_POINT];  // 松弛量
int fx, fy;  // 两个集合中点的数量,可自己改

struct node {  // 这个并不是模板中的一部分,只是为了处理图像中的点
    int pi, pj;
    node() {}
    node(int i, int j) {pi = i, pj = j;}
};

node H[MAX_POINT], M[MAX_POINT];

bool Find(int x) {
    visx[x] = true;
    for (int y = 0; y < fy; y++) {
        if (visy[y]) continue;
        int t = lx[x] + ly[y] - w[x][y];
        if (t == 0) {
            visy[y] = true;
            if (linky[y] == -1 || Find(linky[y])) {
                linky[y] = x;
                return true;
            }
        } else if (slack[y] > t) {
            slack[y] = t;
        }
    }
    return false;
}

int KM() {
    for (int i = 0; i < MAX_POINT; linky[i++] = -1);
    memset(ly, 0, sizeof(ly));
    for (int i = 0; i < fx; i++) {
        lx[i] = -INF;
        for (int j = 0; j < fy; j++) {
            if (w[i][j] > lx[i]) {
                lx[i] = w[i][j];
            }
        }
    }
    for (int x = 0; x < fx; x++) {
        for (int i = 0; i < fy; i++) slack[i] = INF;
        while (1) {
            memset(visx, false, sizeof(visx));
            memset(visy, false, sizeof(visy));
            if (Find(x)) break;
            int d = INF;
            for (int i = 0; i < fy; i++) {
                if (!visy[i] && d > slack[i]) d = slack[i];
            }
            for (int i = 0; i < fx; i++) {
                if (visx[i]) lx[i] -= d;
            }
            for (int i = 0; i < fy; i++) {
                if (visy[i]) ly[i] += d;
                else slack[i] -= d;
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < fy; i++) {
        if (linky[i] > -1) ans += w[linky[i]][i];
    }
    return ans;
}

int main() {

    std::ios::sync_with_stdio(false);

    while (1) {
        
        int h, wid;
        char G[MAX_POINT][MAX_POINT];
        cin >> h >> wid;
        if (h == 0 && wid == 0) break;
        for (int i = 0; i < h; i++) cin >> G[i];
        
        fx = fy = 0;

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < wid; j++) {
                if (G[i][j] == 'm') {
                    M[fx++] = node(i, j);
                } else if (G[i][j] == 'H') {
                    H[fy++] = node(i, j);
                }
            }
        }
        for (int i = 0; i < fx; i++) {
            for (int j = 0; j < fy; j++) {
                w[i][j] = -(abs(M[i].pi - H[j].pi) + abs(M[i].pj - H[j].pj));
            }
        }
        cout << -KM() << endl;
    }

    //getchar();
    //getchar();
    
    return 0;
}              


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值