Sicily 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 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

Solution

给出一张图,点代表通路,H代表house,m代表man,每个人要走到一个屋子,每个人走一步的cost是1,求一人一窝的最小cost是多少。一开始我看题目没有看仔细,以为一个点只能一个人停留,然后就思考怎么避免相撞,那么肯定是需要思考策略和标记走过的路的。后来仔细看题之后,发现对路线没有太多要求,随你怎么走都是可以的,那么就是一个二分图匹配问题了,求完美匹配的最小权重。当然,初始化的权重需要计算,这里直接算距离即可,直接套用最大权重的KM算法模板即可,注意权重和最后的答案均取负数。用的是大白书的O(n^4)的模板,比较好懂。

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

const int maxn = 105;

int home[maxn], men[maxn];
int W[maxn][maxn], n;
int Lx[maxn], Ly[maxn];
int left[maxn];
bool S[maxn], T[maxn];

int abs(int i) { return i > 0 ? i : -i; }

bool match(int i)
{
  S[i] = true;
  for (int j = 1; j <= n; ++j) if (Lx[i] + Ly[j] == W[i][j] && !T[j])
  {
    T[j] = true;
    if (!left[j] || match(left[j]))
    {
      left[j] = i;
      return true;
    }
  }
  return false;
}

void update()
{
  int a = 1<<30;
  for (int i = 1; i <= n; ++i) if (S[i])
    for (int j = 1; j <= n; ++j) if (!T[j])
      a = min(a, Lx[i] + Ly[j] - W[i][j]);
  for (int i = 1; i <= n; ++i)
  {
    if (S[i]) Lx[i] -= a;
    if (T[i]) Ly[i] += a;
  }
}

void KM()
{
  for (int i = 1; i <= n; ++i)
  {
    left[i] = Lx[i] = Ly[i] = 0;
    for (int j = 1; j <= n; ++j)
      Lx[i] = max(Lx[i], W[i][j]);
  }
  for (int i = 1; i <= n; ++i)
  {
    for (;;)
    {
      for (int j = 1; j <= n; ++j) S[j] = T[j] = 0;
      if (match(i)) break;
      else update();
    }
  }
}

int main()
{
  int N, M;
  while (scanf("%d%d", &N, &M) != EOF && !(N == 0 && M == 0) )
  {
    char c, s[105];
    int counth, countm;
    counth = countm = 0;
    memset(W, 0, sizeof(W));

    for (int i = 0; i < N; ++i)
    {
      scanf("%s", s);
      for (int j = 0; j < M; ++j)
      {
        c = s[j];
        if (c == 'H') home[counth++] = i*M + j;
        else if (c == 'm') men[countm++] = i*M + j;
      }
    }
    n = max(counth, countm);
    for (int i = 0; i < countm; ++i)
    {
      for (int j = 0; j < counth; ++j)
      {
        int tx = abs(home[j]/M - men[i]/M);
        int ty = abs(home[j]%M - men[i]%M);
        W[i+1][j+1] = -(tx + ty);
        //printf("(%d-%d) (%d-%d)->%d\n", men[i]/M, men[i]%M, home[j]/M, home[j]%M, W[i][j]);
      }
    }
    KM();
    int res = 0;
    for (int i = 1; i <= n; ++i) res += (Lx[i] + Ly[i]);
    printf("%d\n", -res);
  }

  return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值