Going Home

本文介绍了一种在网格地图上解决最小费用匹配问题的方法。该问题要求计算将多个小人分配到不同房屋中的最低成本,确保每个小人都能进入一个且仅一个房屋,同时每个房屋只能容纳一个小人。通过构建图论中的网络流模型,利用Dinic算法来求解最优解。

Description

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

 

今天仔细听一宁姐的讲解,终于明白dinic邻接表建边了,底下的注释很详细。。。

题意是在n*m的矩阵中中,m是人,H是房子,人和房子一样多,距离是两点之间的距离,就是abs(x1-x0)+abs(y1-y0),然后问所有人到房子的最小费用,建一个源点和汇点,将源点和人相连,容量为1,权值为0,然后将人和房子建边,容量为1,权值为人和房子之间的房子,最后将房子和汇点相连,容量为1,权值为0,。。。

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define oo 1<<28
#include<queue>

using namespace std;
int pre[1000];
int dis[1000];
int vist[1000];
int head[1000];
struct node
{
    int u;
    int v;//与u点相连的点
    int f;//容量
    int w;//权值
    int next;//下一条边
}edge[110000];
struct s
{
    int x;
    int y;
}peo[1000],hou[1000];
int cnt=0;
int s,t;
void add(int u,int v,int f,int w)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].f=f;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].f=0;
    edge[cnt].w=-w;//反向的权值为正向的相反数
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
void init()
{
    memset(pre,-1,sizeof(pre));
    memset(vist,0,sizeof(vist));
    for(int i=0;i<=t;i++)
    {
        dis[i]=oo;
    }
}
int spfa()
{
    int i;
    init();
    queue<int>q;
    dis[s]=0;
    vist[s]=1;
    q.push(s);
    while(!q.empty())//将相邻点进行松弛,直到队列为空
    {
        int u=q.front();//取出队头
        q.pop();
        i=head[u];
        vist[u]=0;
        while(i!=-1)
        {
            int w=edge[i].w;
            int v=edge[i].v;
            if(edge[i].f>0&&dis[v]>dis[u]+w)//判断是否可以更新
            {
                dis[v]=dis[u]+w;//改进s到v点的值
                pre[v]=i;//记录此点的前驱
                if(!vist[v])//由于距离变小了,如果v点松弛成功且v点不在队列里,因为v点有可能还能改进别的点
                {
                    vist[v]=1;
                    q.push(v);
                }
            }
            i=edge[i].next;
        }
    }
    if(pre[t]==-1)//如果不在最短路中代表寻找失败
        return 0;
    return 1;
}
int Cost()
{
    int ans=0;
    while(spfa())//如果增广路寻找成功
    {
        int maxx=oo;
        int p=pre[t];//初始化P指针
        while(p!=-1)
        {
            maxx=min(edge[p].f,maxx);//求出此增广路上边的最小值
            p=pre[edge[p].u];
        }
        p=pre[t];
        while(p!=-1)
        {
            edge[p].f-=maxx;//正向减去
            edge[p^1].f+=maxx;//反向增加
            ans+=maxx*edge[p].w;//因为以单位计费,所以应当乘上流量
            p=pre[edge[p].u];
        }
    }
    return ans;
}
int main()
{
    int n,m,d;
    int i,j;
    char str[1000][1000];
    while(~scanf("%d%d",&n,&m)&&n&&m)
    {
        memset(head,-1,sizeof(head));
        int num=0,num1=0;
        for(i=0; i<n; i++)
        {
            getchar();
            for(j=0; j<m; j++)
            {
                scanf("%c",&str[i][j]);
                if(str[i][j]=='m')
                {
                    add(0,++num,1,0);//将源点和人相连,容量为1,权值为0
                    peo[num].x=i;//记录人的坐标
                    peo[num].y=j;
                }
                if(str[i][j]=='H')
                {
                     ++num1;//记录房子的坐标和个数
                     hou[num1].x=i;
                     hou[num1].y=j;
                }
            }
        }
       //printf("%d %d\n",num,num1);
        s=0,t=num+num1+1;
        for(i=1;i<=num;i++)
        {
            for(j=1;j<=num1;j++)
            {
                d=abs(peo[i].x-hou[j].x)+abs(peo[i].y-hou[j].y);//两点之间的花费
                add(i,j+num,1,d);//将人和房子之间建边,容量为1,权值为d
            }
        }
        for(i=1;i<=num1;i++)
        {
            add(i+num,t,1,0);//将房子和汇点建边,容量为1,权值为0
        }
        printf("%d\n",Cost());
    }
    return 0;
}


 

"Mstar Bin Tool"是一款专门针对Mstar系列芯片开发的固件处理软件,主要用于智能电视及相关电子设备的系统维护与深度定制。该工具包特别标注了"LETV USB SCRIPT"模块,表明其对乐视品牌设备具有兼容性,能够通过USB通信协议执行固件读写操作。作为一款专业的固件编辑器,它允许技术人员对Mstar芯片的底层二进制文件进行解析、修改与重构,从而实现系统功能的调整、性能优化或故障修复。 工具包中的核心组件包括固件编译环境、设备通信脚本、操作界面及技术文档等。其中"letv_usb_script"是一套针对乐视设备的自动化操作程序,可指导用户完成固件烧录全过程。而"mstar_bin"模块则专门处理芯片的二进制数据文件,支持固件版本的升级、降级或个性化定制。工具采用7-Zip压缩格式封装,用户需先使用解压软件提取文件内容。 操作前需确认目标设备采用Mstar芯片架构并具备完好的USB接口。建议预先备份设备原始固件作为恢复保障。通过编辑器修改固件参数时,可调整系统配置、增删功能模块或修复已知缺陷。执行刷机操作时需严格遵循脚本指示的步骤顺序,保持设备供电稳定,避免中断导致硬件损坏。该工具适用于具备嵌入式系统知识的开发人员或高级用户,在进行设备定制化开发、系统调试或维护修复时使用。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值