Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20115   Accepted: 10189

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

题解:这个题意就是,m个人都进入不同的房子的步数和最小;那么求负的步数的最大匹配就可以了;km算法;

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=;
const int INF=1e4;
char s[MAXN][MAXN];
int mp[MAXN][MAXN],lx[MAXN],ly[MAXN],usdx[MAXN],usdy[MAXN],mat[MAXN];
struct Node{
int x,y;
}hou[MAXN],man[MAXN];
int k;
bool dfs(int x){
usdx[x]=;
for(int i=;i<=k;i++){
if(!usdy[i]&&lx[x]+ly[i]==mp[x][i]){
usdy[i]=;
if(!mat[i]||dfs(mat[i])){
mat[i]=x;return true;
}
}
}
return false;
}
int km(){
mem(mat,);mem(lx,);mem(ly,);
for(int i=;i<=k;i++)
for(int j=;j<=k;j++)
lx[i]=max(lx[i],mp[i][j]);
for(int i=;i<=k;i++){
mem(usdx,);mem(usdy,);
while(!dfs(i)){
int d=INF;
for(int j=;j<=k;j++)
if(usdx[j])
for(int b=;b<=k;b++)
if(!usdy[b])
d=min(d,lx[j]+ly[b]-mp[j][b]);
for(int j=;j<=k;j++){
if(usdx[j])lx[j]-=d;
if(usdy[j])ly[j]+=d;
}
mem(usdx,);mem(usdy,);
}
}int ans=;
for(int i=;i<=k;i++)ans+=lx[i]+ly[i];
return INF*k-ans;
}
int main(){
int N,M;
while(scanf("%d%d",&N,&M),N|M){
int k1=,k2=;
for(int i=;i<N;i++){
scanf("%s",s[i]);
for(int j=;j<M;j++){
if(s[i][j]=='H')hou[++k1].x=i,hou[k1].y=j;
if(s[i][j]=='m')man[++k2].x=i,man[k2].y=j;
}
}k=k1;
// printf("%d %d\n",k1,k2);
for(int i=;i<=k;i++)
for(int j=;j<=k;j++)
mp[i][j]=INF-(abs(hou[i].x-man[j].x)+abs(hou[i].y-man[j].y));//这样是因为要找最小的距离,所以把距离变成负的+INF找最大匹配就行了;
printf("%d\n",km());
}
return ;
}

Going Home(最大匹配km算法)的更多相关文章

  1. 带权二分图最大匹配KM算法

    二分图的判定 如果一个图是连通的,可以用如下的染色法判定是否二分图: 我们把X部的结点颜色设为0,Y部的颜色设为1. 从某个未染色的结点u开始,做BFS或者DFS .把u染为0,枚举u的儿子v.如果v ...

  2. UVA1349(带权二分图最大匹配 --> KM算法模板)

    UVA1349 题意:给定一些有向带权边,求出把这些边构造成一个个环,总权值最小 解法: 对于带权的二分图的匹配问题可以用通过KM算法求解. 要求最大权匹配就是初始化g[i][j]为0,直接跑就可以: ...

  3. 二分图的最大匹配以及带权匹配【匈牙利算法+KM算法】

    二分图算法包括 匈牙利算法 与 KM算法. 匈牙利算法 在这里写上模板. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 #include< ...

  4. 浅谈二分图的最大匹配和二分图的KM算法

    二分图还可以,但是我不太精通.我感觉这是一个很烦的问题但是学网络流不得不学它.硬啃吧. 人比较蠢,所以思考几天才有如下理解.希望能说服我或者说服你. 二分图的判定不再赘述一个图是可被划分成一个二分图当 ...

  5. 二分图 最大权匹配 km算法

    这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...

  6. Ural1076(km算法)

    题目大意 给出n*n表格,第a[i,j]表示i到j的权值,现在我们要将每个a[i,j]=sum[j]-a[i,j], 求出当前二分图a[][]最小匹配 最小匹配只需将权值取负后,求二分图最大匹配,使用 ...

  7. km算法的个人理解

    首先相对于上个blog讲的匈牙利算法用于解决无权二分图的最佳匹配,km算法则是在匈牙利算法基础上更进一层的,每条边增加了权值后,真的开始看时有些无厘头,觉得没有什么好方法,但两位牛人Kuhn-Munk ...

  8. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. KM算法专题

    原文:http://972169909-qq-com.iteye.com/blog/1184514 题目地址:这里. 1)求图中所有环的总长度(环的长度不唯一)的最小值.当无法得到完备匹配时说明环不存 ...

随机推荐

  1. C语言字符转换ASCII码

    //函 数 名:CharToHex()//功能描述:把ASCII字符转换为16进制//函数说明://调用函数://全局变量://输    入:ASCII字符//返    回:16进制///////// ...

  2. SDK无法更新

    http://jingyan.baidu.com/article/da1091fbd232fe027949d653.html

  3. Android 开发笔记 “Sqlite Cursor 使用”

    使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生,如果你是搞.net 开发你大可以把Cursor理解成 Ado.net 中的数据集合相当于dataReader.今天特地将它单独拿出来谈, ...

  4. 6.PHP 教程_PHP数据类型

    PHP数据类型 String(字符串),Integer(整型),Float(浮点型),Boolean(布尔型),Array(数组),Object(对象), NULL(空值). PHP字符串 一个字符串 ...

  5. IOS 学习笔记(7) 控件 分隔栏控件(UISegmentControl)的使用方法

    分隔栏控件的系统默认式样一共有3种,分别是“普通式样”,"边框式样","条状式样" 分隔栏控件中有一个momentary属性,默认时NO.当开发者配置成YES时 ...

  6. protel99se中做拼板图解

    很多时候我们要在protel99se中做拼板, 但是通常在复制进行拼版的时候会出现如下的效果,元件被重新命名了. 而无法达到我们需要的像下图的效果 那我们怎么办,才能达到上图的效果呢?其实操作很简单. ...

  7. ImageMagick还是GraphicsMagick?

    引自:http://co63oc.blog.51cto.com/904636/328997 ImageMagick(IM) 套装包含的命令行图形工具是一主要自由软件:Linux,其他类Unix操作系统 ...

  8. 由世纪互联运营的 Windows Azure 现已在中国正式发布

     我们非常高兴地公开发布由世纪互联运营的 Windows Azure,这标志着我们成为第一家在中国国内正式提供公共云平台技术的跨国公司.这一伟大成就的实现,得益于 Microsoft 与世纪互联的 ...

  9. Mark Russinovich 的博客:Windows Azure 主机更新:原因、时间和方式

     Mark Russinovich的技术博客涵盖 Windows故障排除.技术和安全等主题. Windows Azure主机更新:原因.时间和方式 Windows Azure的计算平台(其中包括 ...

  10. Noip2011提高组总结

    这套题思考的难度比较大,应该说是有四题基础题,一题比较复杂的搜索加模拟,还有一题需要深度思考一下.自己的代码漏洞还是很大,而且思考的时候会遗漏一些情况,这些错误都是致命的,去年Noip的惨败也证实了这 ...