HDU-1533Going Home(KM)

                                   Going HomeHDU - 1533  

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.
InputThere 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.  
OutputFor 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

Will not start drawing. Stupid... If you can build a map, you can do a

Code:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define P pair<int ,int >
#define se second
#define fr frist
#define LL long long int
using namespace std;
const int maxn=105;
const int INF=0x3f3f3f;
char a[maxn][maxn];
int mp[maxn][maxn],vis1[maxn],vis2[maxn],slack[maxn],ex1[maxn],ex2[maxn];
int dis[maxn];
struct node
{
    int x;
    int y;
} h[maxn],lie[maxn];
int N,M;
bool dfs(int x)
{
    int gap;
    vis1[x]=1;
    for(int i=0; i<N; i++)
    {
        if(!vis2[i])
        {
            gap=ex1[x]+ex2[i]-mp[x][i];
            if(gap==0)
            {
                vis2[i]=1;
                if(dis[i]==-1||dfs(dis[i]))
                {
                    dis[i]=x;
                    return true;
                }
            }
            else slack[i]=min(slack[i],gap);
        }

    }
    return false;
}
void KM()
{
    mem(dis,-1);
    mem(ex2,0);
    for(int i=0; i<M; i++)
    {
        ex1[i]=mp[i][1];
        for(int j=0; j<N; j++)
        {
            ex1[i]=max(ex1[i],mp[i][j]);
        }
    }
    for(int i=0; i<M; i++)
    {
        mem(slack,INF);
        while(true)
        {
            mem(vis1,0);
            mem(vis2,0);
            if(dfs(i)) break;
            int d=INF;
            for(int j=0; j<N; j++)
                if(!vis2[j]) d=min(slack[j],d);
            for(int j=0; j<M; j++)
                if(vis1[j]) ex1[j]-=d;
            for(int j=0; j<N; j++)
                if(vis2[j]) ex2[j]+=d;
                else slack[j]-=d;
        }
    }
    int ans=0;
    for(int i=0; i<M; i++)
        ans+=mp[dis[i]][i];
    cout<<-ans<<endl;
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m),n+m)
    {
        N=0;
        M=0;
        for(int i=0; i<n; i++)
            scanf("%s",a[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
            {
                if(a[i][j]=='H')
                {
                    h[N].x=i;
                    h[N++].y=j;
                }
                else if(a[i][j]=='m')
                {
                    lie[M].x=i;
                    lie[M++].y=j;
                }
            }
        for(int i=0; i<M; i++)
            for(int j=0; j<N; j++)
            {
                mp[i][j]=abs(lie[i].x-h[j].x)+abs(lie[i].y-h[j].y);
                mp[i][j]=-mp[i][j];
            }
        /*for(int i=0; i<M; i++)
        {
            for(int j=0; j<N; j++)
                cout<<mp[i][j];
            cout<<endl;
        }*/
        KM();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326281664&siteId=291194637