Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2963    Accepted Submission(s): 1492

Problem 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.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3JfMTk5MzA4Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">



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
 
Source

解题思路:

题意为n*m的方格中有p个房子和p个人,人每走一步花费1美元,要求每一个人都要找到一个房子,且每一个房子里仅仅能有一个人,问p个人都找到各自的房子。最小的花费是多少。

能够用KM算法求出最大的花费,要求最小花费仅仅要把邻接矩阵中的权值取反。然后用Km算法最后返回 最大值的相反数就是要求的最小花费。

代码:

#include <iostream>
#include <string.h>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=102;
const int inf=0x3f3f3f3f;
char mp[maxn][maxn];
int n,m;
int npeople;
int g[maxn][maxn];
int nx,ny;
int linked[maxn],lx[maxn],ly[maxn];
int slack[maxn];
bool visx[maxn],visy[maxn]; struct House//存放房子的坐标
{
int x,y;
}house[maxn]; struct Man//存放人的坐标
{
int x,y;
}man[maxn]; bool DFS(int x)//hungary
{
visx[x]=true;
for(int y=0;y<ny;y++)
{
if(visy[y])
continue;
int tmp=lx[x]+ly[y]-g[x][y];
if(tmp==0)
{
visy[y]=true;
if(linked[y]==-1||DFS(linked[y]))
{
linked[y]=x;
return true;
}
}
else if(slack[y]>tmp)
slack[y]=tmp;
}
return false;
} int KM()
{
memset(linked,-1,sizeof(linked));
memset(ly,0,sizeof(ly));
for(int i=0;i<nx;i++)
{
lx[i]=-inf;
for(int j=0;j<ny;j++)
if(g[i][j]>lx[i])
lx[i]=g[i][j];
}
for(int x=0;x<nx;x++)
{
for(int i=0;i<ny;i++)
slack[i]=inf;
while(true)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(DFS(x))
break;
int d=inf;
for(int i=0;i<ny;i++)
if(!visy[i]&&d>slack[i])
d=slack[i];
for(int i=0;i<nx;i++)
if(visx[i])
lx[i]-=d;
for(int i=0;i<ny;i++)
{
if(visy[i])
ly[i]+=d;
else
slack[i]-=d;
}
}
}
int ans=0;
for(int i=0;i<ny;i++)
if(linked[i]!=-1)
ans+=g[linked[i]][i];
return -ans;//返回最大的相反数。即要求的最小
} int main()
{
while(cin>>n>>m&&(n||m))
{
int h1=-1,m1=-1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='H')
house[++h1].x=i,house[h1].y=j;
else if(mp[i][j]=='m')
man[++m1].x=i,man[m1].y=j;
}
npeople=(++h1);
memset(g,inf,sizeof(g));
for(int i=0;i<npeople;i++)//计算邻接矩阵
for(int j=0;j<npeople;j++)
{
int dist=abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y);
g[i][j]=-dist;//权值取反
}
nx=ny=npeople;
cout<<KM()<<endl;
}
return 0;
}

[ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)的更多相关文章

  1. 二分图带权匹配 KM算法与费用流模型建立

    [二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和最大或最小.而二分图的最佳匹配则一定为完备匹配,在此基础上,才要求匹配的边权值之和最大 ...

  2. 二分图最大权匹配——KM算法

    前言 这东西虽然我早就学过了,但是最近才发现我以前学的是假的,心中感慨万千(雾),故作此篇. 简介 带权二分图:每条边都有权值的二分图 最大权匹配:使所选边权和最大的匹配 KM算法,全称Kuhn-Mu ...

  3. HDU 1533:Going Home(KM算法求二分图最小权匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description   On a grid map there ...

  4. [ACM] POJ 3686 The Windy&#39;s (二分图最小权匹配,KM算法,特殊建图)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4158   Accepted: 1777 Descr ...

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

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

  6. HDU2255 奔小康赚大钱 —— 二分图最大权匹配 KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    ...

  7. hdu 2426 Interesting Housing Problem 最大权匹配KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2426 For any school, it is hard to find a feasible ac ...

  8. POJ3565带权匹配——km算法

    题目:http://poj.org/problem?id=3565 神奇结论:当总边权最小时,任意两条边不相交! 转化为求二分图带权最小匹配. 可以用费用流做.但这里学一下km算法. https:// ...

  9. HDU3488 Tour —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit ...

随机推荐

  1. python 条件判断 if

    条件判断 if 格式-1 if 判断条件: 执行语句... num = 10 if num == 4: print("num 等于4") 执行: C:\Python27\pytho ...

  2. hdu 3879 hdu 3917 构造最大权闭合图 俩经典题

    hdu3879  base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点 ...

  3. 一、Ubuntu 简介

    Ubuntu 是一个Linux 系统 Apt-Get apt-get 命令是一个强大的命令行工具,用于同 Ubuntu 的 Advanced Packaging Tool (APT) 一起执行诸如安装 ...

  4. HRBUST 1211 火车上的人数【数论解方程/模拟之枚举+递推】

    火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起(包括第3站 ...

  5. Jumpserver0.5使用说明

    1.系统设置 a.基本设置,这里的ip是jumpserver所在的地址 b.邮件设置,得在qq邮箱中启用授权码 可参考:https://service.mail.qq.com/cgi-bin/help ...

  6. 开篇有益:为什么选择MongoDB?

    为啥用MongoDB? 赶NoSQL时髦? Auto-shard等激动人心的特性? •No! 08年,还都是浮云. 最初的想法是寻找一个可靠的分布式K/V解决MySQL的问题. NoSQL(NoSQL ...

  7. AMD 的 CommonJS wrapping

    其实本文的标题应该是「为什么我不推荐使用 AMD 的 Simplified CommonJS wrapping」,但太长了不好看,为了美观我只能砍掉一截. 它是什么? 为了复用已有的 CommonJS ...

  8. Auto-Test 要点纪录(一)

    1,select下拉框类型 使用工具可以看到html对应标签为<select>这类标签才是真正的下拉框类型就需要对应的方法,不能但看页面上的效果,有的做成了效果但其实不是select类型即 ...

  9. 【java】StringBuilder的三种清除方法对比

    参考链接:https://blog.csdn.net/roserose0002/article/details/6972391

  10. 【java】安全加密MessageDigest的功能及用法【hash一致性算法】

    链接地址:https://blog.csdn.net/ma1kong/article/details/2662997 1.查看MessageDigest源码的注释说明 2.和hash一致性算法 什么关 ...