【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)

题干:

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

题目大意:

在n*m的矩阵格子里有x个人('m')要走到x个房间('H')里,每个房间里只能放一人,人在目前所在的格子处向上下左右相邻的格子走一步就花费1美元,问最后全部的人走到房间后最小的花费。

解题报告:

  直接建图之后最小费用流。

新建超级源点st,超级汇点ed。

st连向每一个m,流量为1,费用为0。

每一个H连向ed,

新增源点from和汇点to,from->人(w为1,cost为0)

房子->to(w为1,cost为0)

每个人->每间房(w为1,cost为最短路径的美元花费)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;const int MAXN = 70000;
const int MAXM = 100005;
const int INF = 0x3f3f3f3f;
struct Edge {int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从 1 ~ N
void init(int n) {N = n;tol = 0;memset(head, -1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost) {edge[tol].to = v;edge[tol].cap = cap;edge[tol].cost = cost;edge[tol].flow = 0;edge[tol].next = head[u];head[u] = tol++;edge[tol].to = u;edge[tol].cap = 0;edge[tol].cost = -cost;edge[tol].flow = 0;edge[tol].next = head[v];head[v] = tol++;
}
bool spfa(int s,int t) {queue<int>q;for(int i = 1; i <= N; i++) {dis[i] = INF;vis[i] = false;pre[i] = -1;}dis[s] = 0;vis[s] = true;q.push(s);while(!q.empty()) {int u = q.front();q.pop();vis[u] = false;for(int i = head[u]; i !=-1; i = edge[i].next) {int v = edge[i].to;if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ) {dis[v] = dis[u] + edge[i].cost;pre[v] = i;if(!vis[v]) {vis[v] = true;q.push(v);}}}}if(pre[t] ==-1)return false;else return true;}
//返回的是最大流,cost 存的是最小费用
int minCostMaxflow(int s,int t,int &cost) {int flow = 0;cost = 0;while(spfa(s,t)) {int Min = INF;for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {if(Min > edge[i].cap-edge[i].flow)Min = edge[i].cap-edge[i].flow;}for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {edge[i].flow += Min;edge[i^1].flow-= Min;cost += edge[i].cost * Min;}flow += Min;}return flow;
}
char s[111][111];
int r[111],c[111];
int R[111],C[111];
int tot1,tot2;
int main()
{int n,m;while(~scanf("%d%d",&n,&m)) {if(n == 0 && m == 0) break;init(n*m+2);memset(dis,0,sizeof dis);tot1=tot2=0;int st=n*m+1,ed=n*m+2;int cost;for(int i = 1; i<=n; i++) {scanf("%s",s[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(s[i][j] == 'm') tot1++,r[tot1] = i,c[tot1] = j;if(s[i][j] == 'H') tot2++,R[tot2] = i,C[tot2] = j;}}for(int i = 1; i<=tot1; i++) {for(int j = 1; j<=tot2; j++) addedge(i,tot1+j,1,abs(c[i]-C[j]) + abs(r[i]-R[j]));}for(int i = 1; i<=tot1; i++) addedge(st,i,1,0);for(int i = 1; i<=tot2; i++) addedge(tot1+i,ed,1,0);int ans = minCostMaxflow(st,ed,cost);printf("%d\n",cost);}	return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440410.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【ZOJ - 4029】Now Loading!!!(整除分块,思维,二分,前缀和)

题干&#xff1a; 其中 zi 是第i次询问后的z。 解题报告&#xff1a; 因为有取log运算&#xff0c;所以分母的取值肯定不会超过30种&#xff0c;所以分每一个分母的时候&#xff0c;用前缀和优化一个和&#xff0c;最后求乘积就行了。&#xff08;其实不需要快速幂&#xff0c…

【ZOJ - 4032】Magic Points (思维,几何,构造)

题干&#xff1a; 解题报告&#xff1a; 想到了&#xff0c;这样绕圈构造。但是这样有个问题&#xff0c;最后一个点如何构造。 刚开始想的是n奇数 &#xff0c; 就8 10 这样的连一条&#xff0c;n偶数 就8 11 这样的连一条&#xff0c;随便构造一下就行&#xff0c;但是发…

android 仿真翻页动画,Android 两Activity之间动画效果(1)---------翻页效果

用Android rotate动画实现翻页效果&#xff0c;效果如图&#xff1a;要实现上面动画&#xff0c;首先搞明白rotate动画原理&#xff1b;(1)Degrees坐标&#xff1a;0度(360度)270度90度 顺时针旋转 180(2)rotate 关键属性fromDegrees 开始旋转时角度 toDegrees 结束时的角…

android 存储不被垃圾清理,手机内存足够大,就不需要清理垃圾了?你错了!

原标题&#xff1a;手机内存足够大,就不需要清理垃圾了?你错了!中新网4月20日电今天,人们使用智能手机的时间已超过电脑,希望在任何时候、任何地方,一部手机搞定所有。对手机的流畅度、性能和安全的要求越来越高。新手机刚到手时非常流畅,用一段时间就出现各种卡顿,网民对猎豹…

android和ios系统的内存,WP和Saipan系统的流畅程度相当于ios,占用的内存很少,但是为什么要用Android取代它...

当涉及到WP和Symbian系统时&#xff0c;许可能没有听说过它&#xff0c;但是对于大多数关注智能手机市场增长的消费者来说&#xff0c;它已经为人们所熟悉&#xff0c;并且许已经使用了它. 当时在功能性机器上使用了Saipan系统&#xff0c;但是您会发现该系统的流畅性与当时的i…

android+微信一键关注,一键关注微信公众平台JS代码有哪些?

一键关注微信公众平台JS代码有哪些&#xff1f;在网页设置一个按钮或者链接可以让用户一键关注微信公众平台&#xff0c;那么这种一键关注微信公众平台的功能如何实现呢&#xff1f;下面小编分享给大家一键关注微信公众平台的JS代码。在微信上&#xff0c;通过微信公众平台推送…

asp.net 写入html代码,asp.net读取模版并写入文本文件

本文要介绍的是ASP.NET怎样读写文本文件&#xff0c;但更重要的是实现的过程。使用的工具是Visual Studio 2015 &#xff0c;.NET版本是4.6.1 。一共建立的2个项目&#xff0c;HoverTreePanel和HoverTreeWeb&#xff0c;都是ASP.NET项目。文章末尾附源码下载。项目结果如下图&a…

html以图像中心定位,在HTML图像上水平和垂直居中文本(绝对定位)

Michael Roach0htmlcssflexbox鉴于以下设计元素,我试图在html中包含图像,以便可以使用css过渡(悬停效果)操纵不透明度.这里的主要缺点是我使用手动垂直居中(绝对/顶部:4​​0%),这在缩小浏览器时变得明显.在使用绝对定位时,是否可以使用flexbox或table进行垂直居中&#xff1f;…

*【CodeForces - 1150D】Three Religions(dp,预处理,思维)

题干&#xff1a; During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now…

华人科学家量子计算机,华人科学家在美国研发出性能强大的光子计算机,能够与中国的量子计算机一战高下!...

原标题&#xff1a;华人科学家在美国研发出性能强大的光子计算机&#xff0c;能够与中国的量子计算机一战高下&#xff01;在最近的《自然纳米技术》杂志上&#xff0c;一篇来自美国哥伦比亚大学的论文在业界掀起了轩然大波&#xff0c;一位名叫虞南方的物理学助理教授成功率领…

【BZOJ - 1001】狼抓兔子(无向图网络流,最小割,或平面图转对偶图求最短路SPFA)

题干&#xff1a; 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到&#xff0c;但抓兔子还是比较在行的&#xff0c; 而且现在的兔子还比较笨&#xff0c;它们只有两个窝&#xff0c;现在你做为狼王&#xff0c;面对下面这样一个网格的地形&#xff1a; …

1.UNIX网络编程卷1:源码配置

本节主要介绍UNIX网络编程卷1&#xff08;第三版&#xff09;在Ubuntu16.04的配置问题&#xff0c;并运行一个简单时间获取客户程序。 1.首先下载源文件&#xff0c;链接如下&#xff1a;UNIX Network Programming Source Code 2.将下载好的压缩文件unpv13e.tar.gz解压&#…

html 地球大气,地球大气层为什么永远不会消失?

地球的大气层是一个很神奇的存在&#xff0c;几十亿年来&#xff0c;它就如同一层厚厚的保护膜&#xff0c;将地球与太空完全阻隔起来&#xff0c;正因为如此&#xff0c;地球上的生命才能够繁衍生息&#xff0c;代代相传。相信很多人都会有这样的疑问&#xff0c;为什么地球上…

【牛客 - 696D】小K的雕塑(dp,鸽巢原理,01背包类问题)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/696/D 来源&#xff1a;牛客网 小K有n个雕塑&#xff0c;每个雕塑上有一个整数 若集合T中的每一个元素在n个雕塑上都能找得到&#xff0c;则称这个集合为一个优秀的集合 小K想知道所有大小<k优秀…

计算机专业需要汇编语言,重点大学计算机专业系列教材·汇编语言程序设计

重点大学计算机专业系列教材汇编语言程序设计语音编辑锁定讨论上传视频本词条缺少概述图&#xff0c;补充相关内容使词条更完整&#xff0c;还能快速升级&#xff0c;赶紧来编辑吧&#xff01;《重点大学计算机专业系列教材汇编语言程序设计》是2009年10月1日清华大学出版社出版…

计算机原理期中考试,计算机组成原理期中考试试题

一、单选题(每小题2分&#xff0c;共34分)1&#xff0e;完整的计算机系统应包括__________。A&#xff0e;运算器、存储器、控制器 B&#xff0e; 主机和实用程序C&#xff0e;配套的硬件设备和软件系统 D&#xff0e; 外部设备和主机2&#xff0e;下列数中真值最小的数是_____…

量子计算机的体积有多大,量子计算机也能实现摩尔定律

原标题&#xff1a;量子计算机也能实现摩尔定律量子计算机拥有很强大的计算力&#xff0c;但是这对IBM来说&#xff0c;似乎还不够。据CNET消息&#xff0c;IBM制作了一个路线图&#xff0c;表达出了自己在量子计算领域的野心。IBM在图表的纵轴上列出了一个单位“量子体积(Quan…

1.How Models work

Introduction 我们首先概述机器学习模型如何工作以及如何使用它们。如果您之前已完成统计建模或机器学习&#xff0c;这可能会感觉很基础。别担心&#xff0c;我们很快就会建立强大的模型。 本课程将为您构建以下场景的模型&#xff1a; 你的堂兄已经花了数百万美元预测房地产…

【POJ - 3020】Antenna Placement (匈牙利算法,二分图最小边覆盖)

题干&#xff1a; The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It …

【POJ - 2195】Going Home(二分图最优匹配,费用流 或 KM)

题干&#xff1a; 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 h…