2195 Going Home

来源:互联网 发布:itunes删除软件 编辑:程序博客网 时间:2024/05/17 04:06
Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8986 Accepted: 4618

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
最大权二分匹配
#include<stdio.h>
#include<string>
#include<cmath>
int g[105][105];//邻接矩阵
int m[105][2];//存储人所在位置的坐标
int h[105][2];//存储房子所在位置的坐标
int lx[105],ly[105];//顶点标号
bool sx[105],sy[105];//是否已经搜索过
int link[105],n;
char ss[105][105];
int min(int a,int b)
{
    if(a<b)  return a;
    return b;
}
bool path(int k)//从x[k]寻找增广路
{
    int i;
    sx[k]=true;
    for(i=0;i<n;i++)
    {
        if(!sy[i]&&(lx[k]+ly[i]==g[k][i]))
        {
             sy[i]=1;
             if(link[i]==-1||path(link[i]))
             {
                  link[i]=k;
                  return true;
             }
        }
    }
    return false;
}
int BestMatch()//求解最小权匹配
{
    int d,sum;
    memset(ly,0,sizeof(ly));
    memset(link,-1,sizeof(link));
    for(int i=0;i<n;i++)
    {
         lx[i]=-1; //x中顶点i的编号为与i关联的Y中边的最大权重
         for(int j=0;j<n;j++)
           if(lx[i]<g[i][j])  lx[i]=g[i][j];
    }
    for(int k=0;k<n;k++)
    {
        while(1)
        {
           memset(sx,0,sizeof(sx));
           memset(sy,0,sizeof(sy));
           if(path(k))  break;//匹配成功
           d=1<<25-1;
           for(int i=0;i<n;i++)
             if(sx[i])
               for(int j=0;j<n;j++)
                 if(!sy[j])
                   d=min(d,lx[i]+ly[j]-g[i][j]);
            for(int i=0;i<n;i++)
            {
                if(sx[i]) lx[i]-=d;
                if(sy[i]) ly[i]+=d;
            }
        }
    }
    sum=0;
    for(int i=0;i<n;i++)  sum+=g[link[i]][i];
    //注意这里本来是求的最大权匹配,这里取反即为最小权匹配(因为临街矩阵中的边的权值也取反了)
    return -sum;
}
int main()
{
    int mi,hi,M,N,ans;
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        if(N==0&&M==0) break;
        mi=hi=0;
        for(int i=0;i<N;i++)
        {
            scanf("%s",ss[i]);
            for(int j=0;j<M;j++)
            {
                if(ss[i][j]=='m')
                {
                    m[mi][0]=i;
                    m[mi][1]=j;
                    mi++;
                }
                else if(ss[i][j]=='H')
                {
                    h[hi][0]=i;
                    h[hi][1]=j;
                    hi++;
                }
            }
        }
        n=mi;
        for(int i=0;i<n;i++)
         for(int j=0;j<n;j++)
           g[i][j]=-(abs(m[i][0]-h[j][0])+abs(m[i][1]-h[j][1]));//即将边的权值取反
        printf("%d/n",BestMatch());
    }
    return 0;
}
原创粉丝点击