hdu 1533 Going Home 【最小權的二分圖匹配】

                                    Going Home
    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Problem 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算法

AC代碼:

# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>

using namespace std;

# define MAXN 1005
# define INF 100000000

int n, m;
int m_count;
int love[MAXN][MAXN];
int ex_man[MAXN];
int ex_house[MAXN];
int vis_man[MAXN];
int vis_house[MAXN];
int match[MAXN];
int slack[MAXN];
int next_dir[4][2] =
{
    { 1, 0 },  //向下走
    { 0, 1 },  //向右走
    { -1, 0 }, //向上走
    { 0, -1 }  //向左走
};
int house_count;

char map[MAXN][MAXN];
struct NODE
{
    int x;
    int y;
    int s;
}que[MAXN * MAXN];

struct POS
{
    int x;
    int y;
}mpos[MAXN], Hpos[MAXN];

int head, tail;
int book[MAXN][MAXN];


//剛開始想的是用bfs搜索進行期望值得計算,但是後來發現不能一一對應
//void bfs(int mannum, int start_x, int start_y)
//{
//  house_count = 0;
//  memset(book, 0, sizeof(book));
//  book[start_x][start_y] = 1;
//  head = 1;
//  tail = 1;
//  que[tail].x = start_x;
//  que[tail].y = start_y;
//  que[tail].s = 0;
//  tail++;
//  int flag = 0;
//  while (head < tail)
//  {
//      for (int k = 0; k < 4; k++)
//      {
//          int tx = que[head].x + next_dir[k][0];
//          int ty = que[head].y + next_dir[k][1];
//          if (tx < 0 || ty < 0 || tx >= n || ty >= m)
//          {
//              continue;
//          }
//          if (!book[tx][ty])
//          {
//              book[tx][ty] = 1;
//              que[tail].x = tx;
//              que[tail].y = ty;
//              que[tail].s = que[head].s + 1;
//              tail++;
//          }
//          if ('h' == map[tx][ty])
//          {
//              love[mannum][house_count] = -que[tail - 1].s;
//              house_count++;
//              if (house_count >= m_count)
//              {
//                  flag = 1;
//                  break;
//              }
//          }
//      }
//      if (flag)
//      {
//          break;
//      }
//      head++;
//  }
//}

bool Dfs(int man)
{
    vis_man[man] = 1;
    for (int house = 0; house < m_count; house++)
    {
        if (vis_house[house])
        {
            continue;
        }

        int gap = ex_man[man] + ex_house[house] - love[man][house];

        if (!gap)
        {
            vis_house[house] = 1;
            if (-1 == match[house] || Dfs(match[house]))
            {
                match[house] = man;
                return true;
            }
        }
        else if (gap < slack[house])
        {
            slack[house] = gap;
        }
    }
    return false;
}

int KM()
{
    int i, j;
    memset(match, -1, sizeof(match));
    memset(ex_house, 0, sizeof(ex_house));
    for (i = 0; i < m_count; i++)
    {
        ex_man[i] = love[i][0];
        for (j = 1; j < m_count; j++)
        {
            if (love[i][j] > ex_man[i])
            {
                ex_man[i] = love[i][j];
            }
        }
    }

    for (i = 0; i < m_count; i++)
    {
        for (j = 0; j < m_count; j++)
        {
            slack[j] = INF;
        }
        while (1)
        {
            memset(vis_house, 0, sizeof(vis_house));
            memset(vis_man, 0, sizeof(vis_man));

            if (Dfs(i))
            {
                break;
            }

            int min = INF;
            for (j = 0; j < m_count; j++)
            {
                if (!vis_house[j])
                {
                    if (slack[j] < min)
                    {
                        min = slack[j];
                    }
                }
            }

            for (j = 0; j < m_count; j++)
            {
                if (vis_man[j])
                {
                    ex_man[j] -= min;
                }
                if (vis_house[j])
                {
                    ex_house[j] += min;
                }
                else
                {
                    slack[j] -= min;
                }
            }
        }
    }
    int res = 0; 
    for (i = 0; i < m_count; i++)
    {
        if (-1 != match[i])
        {
            res += love[match[i]][i];
        }
    }
    return -res; //把值反向
}

void Solve()
{
    int i, j;

    for (i = 0; i < m_count; i++)
    {
        for (j = 0; j < m_count; j++)
        {
            //本題是求最小權的二分圖匹配,把權值反向,則最大值變成最小值,最小值爲最大值,記得輸出時,要把值給復原
            love[i][j] = -(abs(mpos[i].x - Hpos[j].x) + abs(mpos[i].y - Hpos[j].y));
        }
    }

    //用於檢查期望值是否正確
    /*for (i = 0; i < m_count; i++)
    {
        for (j = 0; j < m_count; j++)
        {
            printf("%d ", love[i][j]);
        }
        printf("\n");
    }*/
    printf("%d\n", KM());
}

int main(void)
{
    while (~scanf("%d %d", &n, &m), n && m)
    {
        int i, j;
        m_count = 0;
        int H_count = 0;
        for (i = 0; i < n; i++)
        {
            scanf("%s", map[i]);
            for (j = 0; j < m; j++)
            {
                if ('m' == map[i][j])
                {
                    mpos[m_count].x = i;
                    mpos[m_count].y = j;
                    m_count++;
                }
                if ('H' == map[i][j])
                {
                    Hpos[H_count].x = i;
                    Hpos[H_count].y = j;
                    H_count++;
                }
            }
        }
        Solve();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章