[ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2963 Accepted Submission(s): 1492
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.
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.
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
2
10
28
解题思路:
题意为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算法)的更多相关文章
- 二分图带权匹配 KM算法与费用流模型建立
[二分图带权匹配与最佳匹配] 什么是二分图的带权匹配?二分图的带权匹配就是求出一个匹配集合,使得集合中边的权值之和最大或最小.而二分图的最佳匹配则一定为完备匹配,在此基础上,才要求匹配的边权值之和最大 ...
- 二分图最大权匹配——KM算法
前言 这东西虽然我早就学过了,但是最近才发现我以前学的是假的,心中感慨万千(雾),故作此篇. 简介 带权二分图:每条边都有权值的二分图 最大权匹配:使所选边权和最大的匹配 KM算法,全称Kuhn-Mu ...
- HDU 1533:Going Home(KM算法求二分图最小权匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description On a grid map there ...
- [ACM] POJ 3686 The Windy's (二分图最小权匹配,KM算法,特殊建图)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4158 Accepted: 1777 Descr ...
- 二分图 最大权匹配 km算法
这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...
- HDU2255 奔小康赚大钱 —— 二分图最大权匹配 KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) ...
- 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 ...
- POJ3565带权匹配——km算法
题目:http://poj.org/problem?id=3565 神奇结论:当总边权最小时,任意两条边不相交! 转化为求二分图带权最小匹配. 可以用费用流做.但这里学一下km算法. https:// ...
- HDU3488 Tour —— 二分图最大权匹配 KM算法
题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit ...
随机推荐
- js遍历函数
function each(arr, callback, thisp) { if (arr.forEach) {arr.forEach(callback, thisp);} else { for (v ...
- (4)ASP.NET内置对象1
一.Response 把数据从服务端发送到客户端 Response.Write() 在页面上输出数据 Response.WriteFile(@"F:\WriteFile.txt") ...
- koa2 从入门到进阶之路 (三)
之前的文章我们介绍了一下 koa 路由,get 传值,动态路由,本节我们看一下 koa 中间件 以及 koa 中间件的洋葱图执行流程. 一.什么是 Koa 的中间件 通俗的讲:中间件就是匹配路由之前或 ...
- luogu P1260 工程规划(luogu wa)don't know way
题目描述 造一幢大楼是一项艰巨的工程,它是由n个子任务构成的,给它们分别编号1,2,…,n(5≤n≤1000).由于对一些任务的起始条件有着严格的限制,所以每个任务的起始时间T1,T2,…,Tn并不是 ...
- Elasticsearch搜索结果返回不一致问题
一.背景 这周在使用Elasticsearch搜索的时候遇到一个,对于同一个搜索请求,会出现top50返回结果和排序不一致的问题.那么为什么会出现这样的问题? 后来通过百度和google,发现这是因为 ...
- Jenkins里邮件插件触发器配置和Send to Developers到底是什么意思(转)
邮件触发类型介绍(Triggers) By default, the onlytrigger configured is the "Failure" trigger. To ad ...
- Mac 安装brew和安装composer
一.安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/mast ...
- 内网ip打洞-----p2p实现原理
网上找了非常多.代码大堆,原理讲清楚透彻的不多. 本人找几篇讲得好的来整理一下. 一片技术文章,最基本的讲清楚原理.假设再有完整的能执行的源码也可,关键是要把核心部分代码分析清楚. (1)问题的由来: ...
- 17、Spring Boot普通类调用bean【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...
- hadoop 使用和javaAPI
hadoop的安装,见http://www.powerxing.com/install-hadoop/,简略版教程见http://www.powerxing.com/install-hadoop-si ...