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

  1. 2 2
  2. .m
  3. H.
  4. 5 5
  5. HH..m
  6. .....
  7. .....
  8. .....
  9. mm..H
  10. 7 8
  11. ...H....
  12. ...H....
  13. ...H....
  14. mmmHmmmm
  15. ...H....
  16. ...H....
  17. ...H....
  18. 0 0

Sample Output

  1. 2
  2. 10
  3. 28

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

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. #define mem(x,y) memset(x,y,sizeof(x))
  7. const int MAXN=;
  8. const int INF=1e4;
  9. char s[MAXN][MAXN];
  10. int mp[MAXN][MAXN],lx[MAXN],ly[MAXN],usdx[MAXN],usdy[MAXN],mat[MAXN];
  11. struct Node{
  12. int x,y;
  13. }hou[MAXN],man[MAXN];
  14. int k;
  15. bool dfs(int x){
  16. usdx[x]=;
  17. for(int i=;i<=k;i++){
  18. if(!usdy[i]&&lx[x]+ly[i]==mp[x][i]){
  19. usdy[i]=;
  20. if(!mat[i]||dfs(mat[i])){
  21. mat[i]=x;return true;
  22. }
  23. }
  24. }
  25. return false;
  26. }
  27. int km(){
  28. mem(mat,);mem(lx,);mem(ly,);
  29. for(int i=;i<=k;i++)
  30. for(int j=;j<=k;j++)
  31. lx[i]=max(lx[i],mp[i][j]);
  32. for(int i=;i<=k;i++){
  33. mem(usdx,);mem(usdy,);
  34. while(!dfs(i)){
  35. int d=INF;
  36. for(int j=;j<=k;j++)
  37. if(usdx[j])
  38. for(int b=;b<=k;b++)
  39. if(!usdy[b])
  40. d=min(d,lx[j]+ly[b]-mp[j][b]);
  41. for(int j=;j<=k;j++){
  42. if(usdx[j])lx[j]-=d;
  43. if(usdy[j])ly[j]+=d;
  44. }
  45. mem(usdx,);mem(usdy,);
  46. }
  47. }int ans=;
  48. for(int i=;i<=k;i++)ans+=lx[i]+ly[i];
  49. return INF*k-ans;
  50. }
  51. int main(){
  52. int N,M;
  53. while(scanf("%d%d",&N,&M),N|M){
  54. int k1=,k2=;
  55. for(int i=;i<N;i++){
  56. scanf("%s",s[i]);
  57. for(int j=;j<M;j++){
  58. if(s[i][j]=='H')hou[++k1].x=i,hou[k1].y=j;
  59. if(s[i][j]=='m')man[++k2].x=i,man[k2].y=j;
  60. }
  61. }k=k1;
  62. // printf("%d %d\n",k1,k2);
  63. for(int i=;i<=k;i++)
  64. for(int j=;j<=k;j++)
  65. mp[i][j]=INF-(abs(hou[i].x-man[j].x)+abs(hou[i].y-man[j].y));//这样是因为要找最小的距离,所以把距离变成负的+INF找最大匹配就行了;
  66. printf("%d\n",km());
  67. }
  68. return ;
  69. }

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. web浏览器中的javascript 1

    Html 文档嵌入客户端有4种方式. 1. 内联.放置在<script>和</script>标签对之间. 2.放置在<script>标签的src属性指定的外部文件中 ...

  2. if...else if...else

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prog ...

  3. Jquery $.extend的重载方法详述

    1 $.extend(result,item1,item2,item3,........)  -这个重载方法主要是用来合并,将所有的参数都合并到result中,并返回result,但是这样会破坏res ...

  4. HDU1171:Big Event in HDU(多重背包分析)

    通过分析,要使A>=B并且差值最小.所以只要使sum/2的容量下,B最大就Ok了 #include<iostream> #include<cstdio> #include ...

  5. .NET连接SAP系统专题:.NET调用RFC几种方式(一)

    本来今天是要写一篇关于NCO3.0的东西,就是关乎.NET调用SAP的RFC的,支持VS2010和.NET 4.0等.现在网上到处都是充斥着NCO1.X和NCO2.0,需要用VS2003来使用,都是一 ...

  6. Mysql的四种分区

    mysql一共有四大分区分别为hash range list key 四个分区. 分区的字段需要时主键才可以成功 . 第一种 hash分区 第二张list分区 第三种 key分区 第四种 range分 ...

  7. 什么是RAW?

    RAWRAW是一个PHP网站开发系统,使用简单.快捷,核心功能是通过模版组合网站,模版可以自由开发,使开发者不再受传统开发的那种头晕限制,只需要通过填写表单即可完成网站的开发.此外,开发者还可以通过开 ...

  8. 细说SSO单点登录(转)

    什么是SSO? 如果你已知道,请略过本节! SSO核心意义就一句话:一处登录,处处登录:一处注销,处处注销.即:在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很多人容易把SS ...

  9. 基于Visual C++2013拆解世界五百强面试题--题9-找出所有的排列方式

    给出一个函数来输出一个字符串的所有排列 按照排列组合的知识我们知道 N个字符排列组合个数有n!种, 那么可知f(n) = n*f(n-1), 如果{1,2}的组合有两种,12,21, 那么{123}的 ...

  10. CSS3属性值之box-shadow

    语法:   box-shadow:inset x-offset y-offset blur-radius spread-radius color 也就是:   对象选择器 {box-shadow:投影 ...