//给一个n*m的图,
//m表示人,h表示房子
//问全部人走回家的最小步数
//每一个人仅仅能进一间房
//非常明显的最大带权匹配
//每一个人到每每间房的距离即为权值
//因为是求最小,仅仅要改为负权即可
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
using namespace std ;
const int maxn = 110 ;
const int inf = 0x3f3f3f3f ;
int map[maxn][maxn] ;
int lx[maxn] ,ly[maxn] ;
int slack[maxn] , match[maxn] ;
int visx[maxn] , visy[maxn] ;
char str[maxn][maxn] ;
int mx[maxn] , my[maxn] ;
int hx[maxn] , hy[maxn] ;
int n , m ;
bool find(int x)
{
visx[x] = 1 ;
for(int i = 1;i <= n;i++)
{
if(visy[i])continue ;
int tmp = lx[x] + ly[i] - map[x][i] ;
if(tmp == 0)
{
visy[i] = 1 ;
if(match[i] == -1 || find(match[i]))
{
match[i] = x ;
return true ;
}
}
else slack[i] = min(slack[i] , tmp) ;
}
return false ;
}
int KM()
{
memset(match , -1 , sizeof(match)) ;
memset(ly , 0 , sizeof(ly)) ;
for(int i = 1;i <= n;i++)
{
lx[i] = -inf ;
for(int j = 1;j <= n;j++)
lx[i] = max(lx[i] , map[i][j]) ;
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
slack[j] = inf ;
while(1)
{
memset(visx , 0 ,sizeof(visx)) ;
memset(visy , 0 , sizeof(visy)) ;
if(find(i))break ;
int d = inf ;
for(int j = 1;j <= n;j++)
if(!visy[j])
d = min(slack[j] , d) ;
for(int j = 1;j <= n;j++)
if(visx[j])
lx[j] -= d ;
for(int j = 1;j <= n;j++)
if(visy[j])
ly[j] += d ;
else
slack[j] -= d ;
}
}
int ans = 0 ;
for(int j = 1;j <= n;j++)
ans += map[match[j]][j];
return ans ;
}
int main()
{
// freopen("in.txt" , "r" , stdin) ;
while(scanf("%d%d" , &n , &m) && (n+m))
{
int len_m = 0 , len_h = 0 ;
for(int i = 1;i <= n;i++)
{
scanf("%s" , &str[i][1]) ;
for(int j = 1;j <= m;j++)
if(str[i][j] == 'm')
{
mx[++len_m] = i ;
my[len_m] = j ;
}
else if(str[i][j] == 'H')
{
hx[++len_h] = i ;
hy[len_h] = j ;
}
}
memset(map , 0 ,sizeof(map)) ;
for(int i = 1;i <= len_m;i++)
for(int j = 1;j <= len_h;j++)
map[i][j] = (int)(-abs((double)(mx[i]-hx[j])) - abs((double)(my[i]-hy[j]))) ;
n = len_m ;
int ans = KM() ;
cout<<-ans<<endl;
}
}

最新文章

  1. 重构第29天 去除中间人对象(Remove Middle Man)
  2. nginx-1.4.4 + tcp_proxy_module手动编译安装
  3. JMS - 基本概念
  4. XML解析(转)
  5. (转)基于PHP的cURL快速入门
  6. [LeetCode]Swap Nodes in Pairs 成对交换
  7. DirectShow基础编程 最简单transform filter 编写步骤
  8. SWOT分析是神马?
  9. Java课程设计—象棋
  10. Git的安装和使用(托管至GitHub的方法)
  11. 搭建Jmeter + Grafana + InfluxDB性能测试监控环境
  12. Robot Framework脚本在jenkins执行完之后无法查看日志
  13. LaTeX教程与下载
  14. Java高并发秒杀API之Service层
  15. easyUI详解
  16. 基于Matlab的多自由度系统固有频率及振型计算
  17. 03-MySql安装和基本管理
  18. C#中POST数据和接收的几种方式
  19. yum命令安装软件时,出现--centos 7 安装apache 出现 Could not resolve host: mirrorlist.centos.org; 未知的错误&quot;--CentOS网络设置 couldn&#39;t resolve host &#39;mirrorlist.centos.org问题解决
  20. IIS7Appcmd 命令详解

热门文章

  1. Keyboard input
  2. springmvc 1.接受日期类型的参数 2.后台返回json串的格式处理(返回json串null值处理为&quot;&quot;)
  3. Android项目实战(五十五):部分机型点击home再点图标进入程序不保留再之前界面的问题
  4. &lt;Sicily&gt;Prime Palindromes
  5. [笔记-图论]Floyd
  6. MySql系列之单表查询
  7. webStrom的破解以及汉化
  8. 【Cocos2d-x 3.0】游戏开发之win32配置环境搭建project
  9. 热门游戏&amp;lt;开心消消乐&amp;gt;的“加壳”诡计!!
  10. RequestMapping、Responsebody、RequestBody