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.

输入:

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.

输出:

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

样例输入:

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

样例输出:

2
10
28

题意:

m代表人,H代表房子,每个人要进入一个房子,只能水平或者垂直移动,移动一步需要支付一美元,计算需要支付的最低金额,最小费用最大流。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=101000,INF=0x7fffffff;
int head[maxn],next[maxn<<1],d[maxn],dis[maxn],cnt;
int p[maxn],f[maxn];
int s,t,flow,value;
char ch[105][105];
struct edge
{
    int u,v,w,c;
}node[maxn<<1];
struct NODE
{
    int x,y;
}man[maxn],house[maxn];
void add(int u, int v, int c, int w)
{
    node[cnt].u=u;
    node[cnt].v=v;
    node[cnt].c=c;
    node[cnt].w=w;
    next[cnt]=head[u];
    head[u]=cnt++;
}
bool spfa()
{
    deque<int>q;
    for(int i=0;i<maxn;i++) d[i]=INF;

    memset(dis, 0 ,sizeof (dis));
    memset(p,-1,sizeof(p));
    q.push_front(s) ;
    d[s]=0;
    dis[s]=1;
    p[s]=0;
    f[s]=INF;
    while(!q.empty())
    {
        int u=q.front();q.pop_front();
        dis[u]=0;
        for(int i=head[u];i!=-1;i=next[i])
        {
            edge e=node[i];
            if(d[e.v]>d[e.u]+e.w && e.c > 0)
            {
                d[e.v] = d[e.u] + e.w;
                p[e.v] =i;
                f[e.v] = min(f[u], e.c);
                if(!dis[e.v])
                {
                   if(q.empty())q.push_front(e.v);
                   else
                   {
                       if(d[e.v]<d[q.front()])q.push_front(e.v);
                       else q.push_back(e.v);
                   }
                    dis[e.v]=1;//不要忘记标记
                }
            }
        }
    }
    if(p[t]==-1) return 0;
    flow+=f[t];value+=f[t]*d[t];
    for(int i=t;i!=s;i=node[p[i]].u)
    {
        node[p[i]].c-=f[t];
        node[p[i]^1].c+=f[t];
    }
    return 1;
}
int maxflow()
{
    value=0;
    flow=0;
    while(spfa());
    return value;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m) && n+m)
    {
        getchar();
        memset(head,-1,sizeof(head));
        cnt=0;
        int ans=0;
        int summan=0;//人的个数    
        int sumhouse=0;//房子的个数
        for(int i=0;i<n;i++)
        {
            scanf("%s",ch[i]);
            for(int j=0;j<m;j++)
            {

                if(ch[i][j] == 'm')
                {
                    summan++;
                    man[summan].x = i;//给每个人存下坐标,方便后面求距离
                    man[summan].y = j;
                }
                if(ch[i][j]=='H')
                {
                    sumhouse++;
                    house[sumhouse].x=i;//给每个房子存下坐标,作用同上
                    house[sumhouse].y=j;
                }
            }
        }
        s=0;//超级源点和汇点
        t=summan+sumhouse+1;
        for(int i=1;i<=summan;i++)
        {
            add(s,i,1,0);//源点和人连接。容量为1,费用为0
            add(i,s,0,0);
        }
        for(int i=1;i<=summan;i++)
            for(int j=1;j<=sumhouse;j++)
        {
            int distant=abs(house[j].x-man[i].x)+abs(house[j].y-man[i].y);
            add(i,summan+j,1,distant);//人和房子连接,容量为1,费用为距离
            add(summan+j,i,0,-distant);
        }
        for(int i=1;i<=sumhouse;i++)
        {
            add(summan+i,t,1,0);//房子和汇点连接,容量为1,费用为0
            add(t,summan+i,0,0);
        }
        ans+=maxflow();
        printf("%d\n",ans);
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值