HDU1533-Going Home 简单KM

来源:互联网 发布:northwind数据库下载 编辑:程序博客网 时间:2024/04/30 11:32

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

题意:给n*m的矩阵,矩阵里有代表人的m和代表屋子的H,人要进屋,每个屋子只能进一个人。人在地图上每走一步都会消耗1美元,求最小的消耗(屋子是可以被穿过的)。

思路:简单KM,注意建图时加负权边就可以了。

#include<bits/stdc++.h>using namespace std;#define M 110#define inf 0x3f3f3f3fint link[M];   int visx[M],visy[M];int w[M][M],lx[M],ly[M],slack[M];int ans[M];int h[M][2],m1[M][2],n,m,nx,ny;     int DFS(int x)    {        visx[x] = 1;        for (int y = 1;y <= ny;y ++)        {            if (visy[y])                continue;            int t = lx[x] + ly[y] - w[x][y];            if (t < 1e-5)                   {                visy[y] = 1;                if (link[y] == -1||DFS(link[y]))                {                    link[y] = x;                    return 1;                }            }            else if (slack[y] > t)                  slack[y] = t;        }        return 0;    }    void KM()    {        int i,j;        memset (link,-1,sizeof(link));        memset (ly,0,sizeof(ly));        memset(lx,0,sizeof(lx));        for (i = 1;i <= nx;i ++)                       for (j = 1,lx[i] = -inf;j <= ny;j ++)                if (w[i][j] > lx[i])                    lx[i] = w[i][j];        for (int x = 1;x <= nx;x ++)        {            for (i = 1;i <= ny;i ++)                slack[i] = inf;            while (1)            {                memset (visx,0,sizeof(visx));                memset (visy,0,sizeof(visy));                if (DFS(x))                         break;                  int a=inf;                for(int i=1;i<=nx;i++)if(visx[i])                for(int j=1;j<=ny;j++)if(!visy[j])                a=min(a,lx[i]+ly[j]-w[i][j]);                for(int i=1;i<=nx;i++){                   if(visx[i])lx[i]-=a;                   if(visy[i])ly[i]+=a;                }            }        }        int res = 0;        for(int i = 1;i <= ny;i++)        {            if(link[i] > -1)            res+=w[link[i]][i];        }        printf("%d\n",-res);        }int main(){    //freopen("h1533.in","r",stdin);    while(scanf("%d%d",&n,&m) && (n&&m))    {        char ch;        int km = 1,kh = 1;        for(int i = 1;i <= n;i++)        {            for(int j = 1;j <= m;j++)            {                cin >> ch;                if(ch=='m')                {                    m1[km][0]=i;                    m1[km][1]=j;                    km++;                }                if(ch=='H')                {                    h[kh][0]=i;                    h[kh][1]=j;                    kh++;                }              }        }        for(int i = 1;i < km;i++)        {            for(int j = 1;j < kh;j++)            {                w[i][j] = abs(m1[i][0]-h[j][0])+abs(m1[i][1]-h[j][1]);                w[i][j] = -w[i][j];            }        }        nx = --km;        ny = --kh;        KM();    }   return 0;}
0 0