POJ 2195 Going Home 【二分图最小权值匹配】
传送门:http://poj.org/problem?id=2195
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 26151 | Accepted: 13117 |
Description
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
Output
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
题意概括:
给一个长为 H 宽为 W 的地图,地图上 m 代表人, H 代表家,求所有人回到家的最小权值之和。(人到家的距离为 哈密顿距离)
解题思路:
拆点建二分图,距离反转,KM算法求二分图的最大权值匹配,那么去掉负号最大的就是最小的权值之和了。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = ; char str[MAXN][MAXN];
int c[MAXN][MAXN];
int ex[MAXN], ey[MAXN];
bool visx[MAXN], visy[MAXN];
int match[MAXN];
int HH, WW;
int numx, numy; struct date
{
int x, y;
}M[MAXN], H[MAXN]; bool dfs(int x)
{
visx[x] = true; for(int y = ; y < numx; y++){
if(!visy[y] && ex[x]+ey[y]-c[x][y] == ){
visy[y] = true; if(match[y] == - || dfs(match[y])){
match[y] = x;
return true;
}
}
}
return false;
} void KM()
{
memset(ey, , sizeof(ey));
memset(match, -, sizeof(match)); for(int i = ; i < numx; i++){
ex[i] = c[i][];
for(int j = ; j < numx; j++){
if(c[i][j] > ex[i])
ex[i] = c[i][j];
}
} for(int i = ; i < numx; i++){
while(){
memset(visx, , sizeof(visx));
memset(visy, , sizeof(visy)); if(dfs(i)) break; int d = INF;
for(int j = ; j < numx; j++){
if(visx[j]){
for(int k = ; k < numx; k++){
if(!visy[k] && ex[j]+ey[k]-c[j][k] < d)
d = ex[j] + ey[k] - c[j][k];
}
}
} for(int j = ; j < numx; j++){
if(visx[j]) ex[j]-=d;
if(visy[j]) ey[j]+=d;
}
}
} int res = ;
for(int i = ; i <numx; i++){
res+=c[match[i]][i];
}
printf("%d\n", -res);
} int main()
{
while(~scanf("%d %d", &HH, &WW) && (HH+WW)){
numx = numy = ;
for(int i = ; i < HH; i++){
scanf("%s", &str[i]); for(int j = ; j < WW; j++){
if(str[i][j] == 'm'){
M[numx].x = i;
M[numx++].y = j;
}
else if(str[i][j] == 'H'){
H[numy].x = i;
H[numy++].y = j;
}
}
} for(int i = ; i < numx; i++){
for(int j = ; j < numx; j++)
c[i][j] = -(abs(M[i].x - H[j].x) + abs(M[i].y - H[j].y));
}
KM();
}
return ;
}
POJ 2195 Going Home 【二分图最小权值匹配】的更多相关文章
- POJ-2195 Going Home---KM算法求最小权值匹配(存负边)
题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...
- POJ 3565 Ants 【最小权值匹配应用】
传送门:http://poj.org/problem?id=3565 Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: ...
- ZOJ-2342 Roads 二分图最小权值覆盖
题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...
- UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi ...
- 紫书 例题11-10 UVa 1349 (二分图最小权完美匹配)
二分图网络流做法 (1)最大基数匹配.源点到每一个X节点连一条容量为1的弧, 每一个Y节点连一条容量为1的弧, 然后每条有向 边连一条弧, 容量为1, 然后跑一遍最大流即可, 最大流即是最大匹配对数 ...
- UVA 1349 Optimal Bus Route Design (二分图最小权完美匹配)
恰好属于一个圈,那等价与每个点有唯一的前驱和后继,这让人想到了二分图, 把一个点拆开,点的前驱作为S集和点的后继作为T集,然后连边,跑二分图最小权完美匹配. 写的费用流..最大权完美匹配KM算法没看懂 ...
- POJ 2404 Jogging Trails(最小权完美匹配)
[题目链接] http://poj.org/problem?id=2404 [题目大意] 给出一张图,求走遍所有的路径至少一次,并且回到出发点所需要走的最短路程 [题解] 如果图中所有点为偶点,那么一 ...
- POJ 2195 Going Home (带权二分图匹配)
POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...
- [ACM] POJ 3686 The Windy's (二分图最小权匹配,KM算法,特殊建图)
The Windy's Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4158 Accepted: 1777 Descr ...
随机推荐
- ELK 插件(一) ---- head
一, 插件介绍 01, ElasticSearch Head是什么? ElasticSearch Head是集群管理.数据可视化.增删查改.查询语句可视化工具.可以对集群进行傻瓜式操作.你可以通过插件 ...
- Hadoop升级
1.停止所有MR任务 stop-mapred.sh 2.检查HDFS hadoop fsck / -files -blocks -locations > dfs-v-old-fsck-.log ...
- 03-struts2获得servetAPI
1 原理 三个域合一的时候相同的键值对以小的域为准.ActionContext 对象创建:每次请求的时候都会创建一个与请求对应的 ActionContext 对象.ActionContext 销毁:请 ...
- Mysql系列-字符集
字符集 怎样选择合适的字符集 如果应用程序需要发布到很多国家和地区,需要支持各种各样的文字,则选择Unicode编码,Mysql中即UTF-8.q如果需要将数据导入数据库,这时候要注意数据库字符集对数 ...
- 安装 fedora 之后
下载zsh,并安装oh-my-zsh dnf install zsh -y 到github的oh-my-zsh上按照教程安装oh-my-zsh,配置主题为minimal,这个是个人比较喜欢的主题,因为 ...
- K&R C风格函数
前些天在看getopt源码, 一种前所未见的函数定义方法 int getopt(nargc, nargv, ostr) int nargc; char * const nargv[]; const c ...
- Table 边框合并(collapse)
border-collapse:collapse 用于表格属性, 表示表格的两边框合并为一条; <style type="text/css"> table { bord ...
- C# ADO.NET
ADO.NET 作业总结难点 数据库语句掌握太差 //查询 select * from Users //查询表中所有数据 select * from Users where UserName = 'l ...
- mac解决系统设置安全与隐私没有允许所有来源
解决系统设置安全与隐私没有允许所有来源:sudo spctl --master-disable
- Csharp:TinyMCE HTML Editor in .NET WindowsForms
/// <summary> /// /// </summary> public partial class Form2 : Form { private mshtml.IHTM ...