首页 > ACM题库 > HDU-杭电 > HDU 1533 Going Home-二分图-[解题报告] C++
2013
12-12

HDU 1533 Going Home-二分图-[解题报告] C++

Going Home

问题描述 :

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.

输入:

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.

输出:

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

样例输入:

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

样例输出:

2
10
28

HDU_1533

    由于图上每个点都可达,所以人和房子间的最短距离就是曼哈顿距离。建好图之后应用KM算法求二分图最优匹配即可。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXD 110
#define MAX 10010
#define INF 1000000000
int N, hx[MAXD], hy[MAXD], px[MAXD], py[MAXD];
int a[MAXD][MAXD], G[MAXD][MAXD], yM[MAXD];
int A[MAXD], B[MAXD], slack, visx[MAXD], visy[MAXD];
char b[MAXD];
int init()
{
    int i, j, k, n, m, pn, x, y, newx, newy, front, rear;
    scanf("%d%d", &n, &m);
    if(!n && !m)
        return 0;
    N = pn = 0;
    for(i = 0; i < n ;i ++)
    {
        scanf("%s", b);
        for(j = 0; j < m; j ++)
        {
            if(b[j] == 'H')
            {
                a[i][j] = 0;
                hx[N] = i;
                hy[N] = j;
                N ++;
            }
            else if(b[j] == 'm')
            {
                a[i][j] = 1;
                px[pn] = i;
                py[pn] = j;
                pn ++;
            }
            else
                a[i][j] = 1;
        }
    }
    memset(G, 0, sizeof(G));
    for(i = 0; i < N; i ++)
        for(j = 0; j < N; j ++)
            G[i][j] = MAX - (abs(px[i] - hx[j]) + abs(py[i] - hy[j]));
    return 1;
}
int searchpath(int u)
{
    int v, temp;
    visx[u] = 1;
    for(v = 0; v < N; v ++)
        if(!visy[v])
        {
            temp = A[u] + B[v] - G[u][v];
            if(temp == 0)
            {
                visy[v] = 1;
                if(yM[v] == -1 || searchpath(yM[v]))
                {
                    yM[v] = u;
                    return 1;
                }
            }
            else if(temp < slack)
                slack = temp;
        }
    return 0;
}
void EK()
{
    int i, j, u;
    for(i = 0; i < N; i ++)
    {
        A[i] = 0;
        for(j = 0; j < N; j ++)
        {
            if(G[i][j] > A[i])
                A[i] = G[i][j];
        }
    }
    memset(B, 0, sizeof(B));
    memset(yM, -1, sizeof(yM));
    for(u = 0; u < N; u ++)
        for(;;)
        {
            memset(visx, 0, sizeof(visx));
            memset(visy, 0, sizeof(visy));
            slack = INF;
            if(searchpath(u))
                break;
            for(i = 0; i < N; i ++)
            {
                if(visx[i])
                    A[i] -= slack;
                if(visy[i])
                    B[i] += slack;
            }
        }
}
void printresult()
{
    int i, res = 0;
    for(i = 0; i < N; i ++)
        res += MAX - G[yM[i]][i];
    printf("%d\n", res);
}
int main()
{
    while(init())
    {
        EK();
        printresult();
    }
    return 0;
}


解题报告转自:http://www.cnblogs.com/staginner/archive/2011/10/07/2200729.html