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

本文探讨了一个人员分配到房屋的场景,通过构建网络流模型,使用最小费用流算法来解决如何以最少成本将人员从起始位置移动到目标房屋的问题。算法首先建立图结构,连接人员、房屋及超级源点和汇点,然后运行网络流求解最小费用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值