HDU 1533 Going Home【km应用】

来源:互联网 发布:ff14拉拉菲尔捏脸数据 编辑:程序博客网 时间:2024/05/16 08:33

HDU 1533 Going Home【km应用】http://acm.hdu.edu.cn/showproblem.php?pid=1533

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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
 

Sample Output
21028
 

Source
Pacific Northwest 2004
 
题目大意:

给你一个N行M列的矩阵,其中“.”代表空地,“H”代表房子,“m”代表人,其中有n个房子和n个人。现在要求每个人进入一间房子,且人走一步需要支付1美元。

求最小需要花费多少美元才能让所有人都进入到房子中(每个人只能进入一间房子,每个房子只能容纳一个人)。

解题思路:

这道题其实就是二分图最优匹配的变形而已。

因为要求的其实是最小权值之和。而KM算法求的是最大权值之和。

为此,我们可以有两种方法来解决:

1.在建图的时候,将每条边的权值变为负数。然后lx[i]初始化为-INT_MAX,结果输出-ans,就可以得到最小权值。

2.在建图的时候,用一个相对的大数减去每个权值,比如现在边为6,8,10.我用100来减,得到的边为94,92,90.最后输出时,用100*n-ans也可以得到最小的权值。

方法1代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <cmath>using namespace std;const int N = 310;char mmap[N][N];int map[N][N],slack[N],match[N],lx[N],ly[N];bool visx[N],visy[N];int n;bool dfs(int x){    visx[x] = true;    for(int i=0; i<n; i++)    {        if(visy[i]) continue;        int t = lx[x] + ly[i] - map[x][i];        if(!t)        {            visy[i] = true;            if(match[i] == -1 || dfs(match[i])){                match[i] = x;                return true;            }        }        else            slack[i] = min(slack[i], t);    }    return false;}void km(){    int temp;    memset(lx,0,sizeof(lx));    memset(ly,0,sizeof(ly));    memset(match,-1,sizeof(match));    for(int i=0; i<n; i++)        for(int j=0; j<n; j++)            lx[i] = max(lx[i],map[i][j]);    for(int i=0; i<n; i++)    {        for(int j=0; j<n; j++) slack[j] = INT_MAX;        while(1)        {            memset(visx,0,sizeof(visx));            memset(visy,0,sizeof(visy));            if(dfs(i)) break;            else{                temp = INT_MAX;                for(int j=0; j<n; j++)                {                    if(!visy[j] && temp>slack[j])                        temp = slack[j];                }                for(int j=0; j<n; j++)                {                    if(visx[j]) lx[j] -= temp;                    if(visy[j]) ly[j] += temp;                    else slack[j] -= temp;                }            }        }    }}int main(){    int row,col,ans,numi,numj;    while(scanf("%d %d",&row,&col)!=EOF)    {        if(row == 0 && col == 0) break;        n = ans = numi = numj = 0;        for(int i=0; i<row; i++){            scanf("%s",mmap[i]);            for(int j=0; j<col; j++)                if(mmap[i][j] == 'm') n++;        }        for(int i=0; i<row; i++){            for(int j=0; j<col; j++){                if(mmap[i][j] == 'm')                {                    for(int k=0; k<row; k++){                        for(int l=0; l<col; l++){                            if(mmap[k][l] == 'H')                                map[numi][numj++] =  - (abs(k - i) + abs(l - j));//变为负权边                        }                    }                    numi++;                    numj = 0;                }            }        }        km();        for(int i=0; i<n; i++)            ans += map[ match[i] ][i];        printf("%d\n", - ans);//结果取反    }    return 0;}





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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
 

Sample Output
21028
 

Source
Pacific Northwest 2004
 

0 0
原创粉丝点击