题面

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

题解

题目大意:给定一张地图,m是人,H是房子,现在每个人都要到一栋房子去,问所有人到房子的曼哈顿距离之和最小是多少?
题解:
可以用KM做
考虑用费用流,
每个人向房子连一条容量为1,费用为曼哈顿距离的边
求最小费用流即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAX 200
#define MAXL 200000
#define INF 1000000000
struct Line
{
    int v,next,w,fb,fy;
}e[MAXL];
int ff,hh,mm,ww;
struct Node
{
    int x,y;
};
vector<Node> H,M;
int h[MAX*MAX],cnt=1,cost;
int pe[MAX*MAX],pr[MAX*MAX];
inline void Add(int u,int v,int w,int fy)
{
    e[cnt]=(Line){v,h[u],w,cnt+1,fy};
    h[u]=cnt++;
    e[cnt]=(Line){u,h[v],0,cnt-1,-fy};
    h[v]=cnt++;
}
int dis[MAX*MAX],S,T,n,m;
char g[MAX][MAX];
bool vis[MAX*MAX];
bool SPFA()
{
    memset(dis,63,sizeof(dis));
    dis[S]=0;
    queue<int> Q;while(!Q.empty())Q.pop();
    Q.push(S);
    memset(vis,0,sizeof(vis));
    while(!Q.empty())
    {
        int u=Q.front();Q.pop();
        vis[u]=false;
        for(int i=h[u];i;i=e[i].next)
        {
            int f=dis[u]+e[i].fy,v=e[i].v;
            if(e[i].w&&dis[v]>f)
            {
                dis[v]=f;
                pe[v]=i;
                pr[v]=u;
                if(!vis[v])
                {
                    vis[v]=true;
                    Q.push(v);
                }
            }
        }
    }
    if(dis[T]==dis[T+1])return false;//增广失败
    int re=INF;
    for(int v=T;v!=S;v=pr[v])
        re=min(re,e[pe[v]].w);//计算增广的最大流
    for(int v=T;v!=S;v=pr[v])
    {
        e[pe[v]].w-=re;
        e[e[pe[v]].fb].w+=re;
    }
    ff+=re;
    cost+=re*dis[T];
    return true;
}
int main()
{
    //freopen("POJ2195.in","r",stdin);
    while(233)
    {
        scanf("%d%d",&n,&m);
        cnt=1;
        memset(h,0,sizeof(h));
        if(n==0&&m==0)break;
        H.clear();M.clear();hh=mm=cost=ff=0;
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j)
            {
                cin>>g[i][j];
                if(g[i][j]=='H')
                {
                    H.push_back((Node){i,j});
                    hh++;
                }
                if(g[i][j]=='m')
                {
                    M.push_back((Node){i,j});
                    mm++;
                }
            }
        S=0;T=mm+hh+1;
        for(int i=0;i<M.size();++i)
        {
            Add(S,i+1,1,0);
            for(int j=0;j<H.size();++j)
            {
                Add(i+1,1+j+mm,1,abs(M[i].x-H[j].x)+abs(M[i].y-H[j].y));//曼哈顿距离
            }
        }
        for(int i=1;i<=hh;++i)
            Add(i+mm,T,1,0);
        while(SPFA());
        printf("%dn",cost);
    }
    return 0;
}
内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!