POJ - 2195 Going Home (网络流最小流)

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为房子的位置,找到所有人都可以进入房子的最小距离。

思路:网络流最小流模板题,先建图,每一个人与房子的连线,超级源点与人的连线,房子与超级汇点的连线,跑一遍网络流。


#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<queue>
#define inf 0x3f3f3f3f
#define FOR(a,b) for(int a=0;a<b;a++)
using namespace std;
int n,m,cut,HH,MM,head[20010];
char str[110][110];
struct dot
{
    int x,y;
} H[10010],M[10010];
struct node
{
    int to,l,v,nextt;
} A[100010];
void add(int x,int y,int l,int v)
{
    A[cut].l=l;
    A[cut].v=v;
    A[cut].to=y;
    A[cut].nextt=head[x];
    head[x]=cut++;
    A[cut].l=0;
    A[cut].v=-v;
    A[cut].to=x;
    A[cut].nextt=head[y];
    head[y]=cut++;
}
void init()
{
    cut=HH=MM=0;
    FOR(i,n)
    {
        scanf("%s",str[i]);
        FOR(j,m)
        {
            if(str[i][j]=='H')
            {
                H[++HH].x=i;
                H[HH].y=j;
            }
            if(str[i][j]=='m')
            {
                M[++MM].x=i;
                M[MM].y=j;
            }
        }
    }
    memset(head,-1,sizeof(head));
    for(int i=1; i<=MM; i++)
        for(int j=1; j<=HH; j++)
        {
            add(i,j+MM,1,abs(M[i].x-H[j].x)+abs(M[i].y-H[j].y));
        }
    for(int i=1; i<=MM; i++)
    {
        add(0,i,1,0);
    }
    for(int j=1; j<=HH; j++)
        add(j+MM,MM+HH+1,1,0);
}
int dis[20010],vis[20010],path[20010],flow[20010],dots[20010];
queue<int>q;
int spfa(int be,int to)
{
    memset(flow,0,sizeof(flow));
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    q.push(be);
    dis[be]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        int k=head[u];
        while(k!=-1)
        {
            int v=A[k].to;
            if(A[k].l)
            {
                if(dis[v]>dis[u]+A[k].v)
                {
                    dis[v]=dis[u]+A[k].v;
                    path[v]=u;
                    dots[v]=k;
                    if(!vis[v])
                    {
                        q.push(v);
                        vis[v]=1;
                    }
                }
            }
            k=A[k].nextt;
        }
    }
    return dis[to]!=inf;
}
int MCMF(int be,int to)
{

    int ans=0,f=inf;
    while(spfa(be,to))
    {
        for(int i=to; i; i=path[i])
        {
            f=min(A[dots[i]].l,f);
        }
        for(int i=to; i; i=path[i])
        {
            ans+=f*A[dots[i]].v;
            A[dots[i]].l-=f;
            A[dots[i]^1].l+=f;
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m),n||m)
    {
        init();
        printf("%d\n",MCMF(0,MM+HH+1));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/81745312
今日推荐