【POJ - 2195】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的矩阵,' . ' 表示土地,' m '表示一个人,' H '代表一个屋子,现在告诉你屋子数和人数相同,要让所有人进入屋子内,且一个屋子只能装一个人,人移动一格花费是1,且只能上下左右移动,那么问你总的最小花费是多少。

解题报告:

人连到起点,房子连到终点,每一个格子可以上下左右四周走,求一个二分图最优匹配就行了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
#include <iomanip>
using namespace std;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct node {int to,c,w,ne;
} e[50005<<2];
int n,m,N;
int head[MAX],d[MAX],vis[MAX],tot,p[MAX];
void add(int u,int v,int c,int cost=0) {e[++tot].to = v;e[tot].c = c;e[tot].w = cost;e[tot].ne = head[u];head[u] = tot;e[++tot].to = u;e[tot].c = 0; e[tot].w = -cost;e[tot].ne = head[v];head[v] = tot;
}
bool bfs(int s,int t) {for(int i = 0; i<=N; i++) d[i]=INF,vis[i]=0;d[s]=0;queue<int>q;q.push(s);while(!q.empty()) {int u=q.front();q.pop();vis[u]=0;for(int i=head[u]; ~i; i=e[i].ne) {int j=e[i].to;if(e[i].c&&d[j]>d[u]+e[i].w) {d[j]=d[u]+e[i].w;p[j]=i;if(!vis[j])vis[j]=1,q.push(j);}}}return d[t]<INF;
}
int MCMF(int s,int t,int &flow) {ll ans=0;while(bfs(s,t)) {int x=t,f=INF;while(x!=s) {f = min(f,e[p[x]].c),x=e[p[x]^1].to;}flow += f;ans+=1LL*d[t]*f;x=t;while(x!=s) {e[p[x]].c-=f,e[p[x]^1].c+=f;x=e[p[x]^1].to;}}return ans;
}
void init(int NN) {tot=1;for(int i = 0; i<=NN; i++) head[i] = -1;
}
char s[105][105];
int id(int i,int j) {return (i-1)*m+j;
}
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
int main() 
{while(~scanf("%d%d",&n,&m)) {if(n+m==0) break;int st=0,ed=n*m+1;N=n*m+1; init(n*m+1);for(int i = 1; i<=n; i++) {scanf("%s",s[i]+1);for(int j = 1; j<=m; j++) {if(s[i][j] == 'm') add(st,id(i,j),1,0);if(s[i][j] == 'H') add(id(i,j),ed,1,0); }}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {for(int k = 0; k<4; k++) {int tx = i+nx[k];int ty = j+ny[k];if(tx<1||tx>n||ty<1||ty>m) continue;add(id(i,j),id(tx,ty),INF,1);}}}int ansf=0;int ansc=MCMF(st,ed,ansf);printf("%d\n",ansc);	}return 0 ;	
}

 

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

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

相关文章

微机原理实验8254计算机钢琴,GitHub - SincereXIA/PianoMFC: 西电微机原理课设项目,键盘电子乐器演奏程序设计(电子琴),MFC...

PianoMFC西电微机原理课设项目&#xff0c;键盘电子乐器演奏程序设计(电子琴)&#xff0c;MFC需要连接西电微机原理实验室提供的 QTH9054 微机试验箱&#xff0c;使用其蜂鸣器发声&#xff0c;若不连接&#xff0c;程序会直接播放 mp3 文件模拟钢琴声。请在 release 处下载编译…

5.Underfitting and Overfitting

在这一步结束时&#xff0c;您将了解欠拟合和过拟合的概念&#xff0c;并且您将能够应用这些办法来使您的模型更准确。 Experimenting With Different Models 现在您已经有了一种可靠的方法来测量模型精度&#xff0c;您可以尝试使用其他模型&#xff0c;并查看哪种模型可以提…

福建省计算机初级职称,2019福建助理工程师职称评定条件材料及审核管理制度...

一学历、资历条件要求(破格申报不在此列&#xff0c;详情请咨询了解)申报工程技术系列中级工程师须符合下列条件之一&#xff1a;1.博士研究生毕业&#xff1b;2.硕士研究生毕业后&#xff0c;从事所申报专业工作满3年&#xff1b;3.本科毕业后&#xff0c;从事所申报专业工作满…

【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)

题干&#xff1a; Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings…

6.Random Forests

Introduction 决策树会让您做出艰难的决定。 有很多树叶的深树将会过拟合&#xff0c;因为每个预测都来自其叶子上只有少数房屋的历史数据。 但是叶子很少的浅树会表现不佳&#xff0c;因为它无法捕获原始数据中的许多区别。 即使在今天&#xff0c;最成熟的建模技术也面临着过…

7.Handling Missing Values

本教程是学习机器学习课程的第2部分。 本教程选择了1级完成的位置&#xff0c;因此如果您从1级完成练习&#xff0c;您将获得最大的收益。 在此步骤中&#xff0c;您将学习三种处理缺失值的方法。 然后&#xff0c;您将学习如何比较这些方法在任何给定数据集上的有效性。 Intr…

打开电脑计算机超级慢,手把手教你电脑开机慢怎么办

等到花都谢了&#xff0c;你怎么还不开机&#xff1f;这电脑开机真是离奇的慢&#xff0c;有心将它换了&#xff0c;奈何兜里空空。凑合着用又无法忍受这种煎熬。其实你只需要用鼠标点几下就可以不用等待这漫长的开机过程了。高铁&#xff0c;飞机&#xff0c;网络&#xff0c;…

【POJ - 1486】Sorting Slides(思维建图,二分图求必须边,关建边,图论)

题干&#xff1a; Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he is not a very tidy person and has put all his transparencies on one big heap. Before giving the talk, he has to sort the slides. Being a kind of minima…

用OpenSSL编写SSL,TLS程序

http://zhoulifa.bokee.com/6134045.html http://blog.sina.com.cn/s/blog_86ca13bb0100vaph.html http://blog.chinaunix.net/uid-26575352-id-3048856.html 一、简介: SSL(SecureSocket Layer)是netscape公司提出的主要用于web的安全通信标准,分为2.0版和3.0版.TLS(Transport…

信息技术计算机伦理与安全教案,龙教版信息技术七年级下册第7课 安全与道德 教案...

ID:9954219分类&#xff1a;全国,2019资源大小&#xff1a;228KB资料简介:题 目第七课 安全与道德总课时1设计来源自我设计教学时间教材分析这节课计算机与网络安全部分定义介绍和叙述较多,所以为了避免枯燥可以设计课件和并准备病毒计算机安全报道的视频、多媒体讲解、图片等…

【HDU - 5706】GirlCat(bfs)

题干&#xff1a; As a cute girl, Kotori likes playing Hide and Seek with cats particularly. Under the influence of Kotori, many girls and cats are playing Hide and Seek together. Koroti shots a photo. The size of this photo is nmnm, each pixel of the ph…

8.Using Categorical Data with One Hot Encoding

本教程是机器学习系列的一部分。 在此步骤中&#xff0c;您将了解“分类”变量是什么&#xff0c;以及处理此类数据的最常用方法。 Introduction 分类数据是仅采用有限数量值的数据。 例如&#xff0c;如果人们回答一项关于他们拥有哪种品牌汽车的调查&#xff0c;结果将是明…

iPhone换屏幕测试软件,怎样检验iPhone是否更换过屏幕?

原标题&#xff1a;怎样检验iPhone是否更换过屏幕&#xff1f;关注下图公众号&#xff0c;鉴定苹果手机真假↓↓↓购买新手机时&#xff0c;到手后会想手机各零部件是否是正品原装&#xff0c;就好比屏幕是否原装屏&#xff01;入手一部iPhone新机的时候&#xff0c;该如何检验…

*【HDU - 5707】Combine String(dp)

题干&#xff1a; Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of aa and bb. A string cc is said to be the combine string of aa and bb if and only if cc can be broken into two subsequences, when you read the…

《TCP/IP详解》学习笔记(一):基本概念

为什么会有TCP/IP协议 在世界上各地&#xff0c;各种各样的电脑运行着各自不同的操作系统为大家服务&#xff0c;这些电脑在表达同一种信息的时候所使用的方法是千差万别。就好像圣经中上帝打乱 了各地人的口音&#xff0c;让他们无法合作一样。计算机使用者意识到&#xff0c;…

【POJ - 3272】Cow Traffic(dp,建反向图,DAG拓扑图)

题干&#xff1a; The bovine population boom down on the farm has caused serious congestion on the cow trails leading to the barn. Farmer John has decided to conduct a study to find the bottlenecks in order to relieve the traffic jams at milking time. The…

pc服务器不同型号,服务器与PC系统软件之不同

服务器与PC系统软件之不同对于中关村在线的网友来说&#xff0c;PC系统应该都不陌生&#xff0c;而且分分钟重装的水准。但在笔者过往的服务器装机经验中&#xff0c;可谓是一部千年血泪史。服务器和PC系统差别还是很大的。现在的PC系统多是windows7和windows10&#xff0c;而在…

9.XGBoost

本教程是机器学习系列的一部分。 在此步骤中&#xff0c;您将学习如何使用功能强大的xgboost库构建和优化模型。 What is XGBoost XGBoost是处理标准表格数据的领先模型&#xff08;您在Pandas DataFrames中存储的数据类型&#xff0c;而不是像图像和视频这样的更奇特的数据类…

*【HDU - 5711】Ingress(tsp旅行商问题,优先队列贪心,状压dp,floyd最短路,图论)

题干&#xff1a; Brickgao, who profited from your accurate calculating last year, made a great deal of money by moving bricks. Now he became gay shy fool again and recently he bought an iphone and was deeply addicted into a cellphone game called Ingress. …

ajax get请求成功,成功()函数的AJAX GET请求

后不叫我有一个jQuery的AJAX脚本像下面&#xff1a;成功()函数的AJAX GET请求function FillCity() {var stateID $("#ddlState").val();$.ajax({url: Url.Action("Employee", "Index"),type: "GET",dataType: "json",data:…