传送门:http://poj.org/problem?id=2195

Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26151   Accepted: 13117

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

题意概括:

给一个长为 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 【二分图最小权值匹配】的更多相关文章

  1. POJ-2195 Going Home---KM算法求最小权值匹配(存负边)

    题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...

  2. POJ 3565 Ants 【最小权值匹配应用】

    传送门:http://poj.org/problem?id=3565 Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: ...

  3. ZOJ-2342 Roads 二分图最小权值覆盖

    题意:给定N个点,M条边,M >= N-1.已知M条边都有一个权值,已知前N-1边能构成一颗N个节点生成树,现问通过修改这些边的权值使得最小生成树为前N条边的最小改动总和为多少? 分析:由于计算 ...

  4. UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design

    题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi  ...

  5. 紫书 例题11-10 UVa 1349 (二分图最小权完美匹配)

    二分图网络流做法 (1)最大基数匹配.源点到每一个X节点连一条容量为1的弧, 每一个Y节点连一条容量为1的弧, 然后每条有向 边连一条弧, 容量为1, 然后跑一遍最大流即可, 最大流即是最大匹配对数 ...

  6. UVA 1349 Optimal Bus Route Design (二分图最小权完美匹配)

    恰好属于一个圈,那等价与每个点有唯一的前驱和后继,这让人想到了二分图, 把一个点拆开,点的前驱作为S集和点的后继作为T集,然后连边,跑二分图最小权完美匹配. 写的费用流..最大权完美匹配KM算法没看懂 ...

  7. POJ 2404 Jogging Trails(最小权完美匹配)

    [题目链接] http://poj.org/problem?id=2404 [题目大意] 给出一张图,求走遍所有的路径至少一次,并且回到出发点所需要走的最短路程 [题解] 如果图中所有点为偶点,那么一 ...

  8. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

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

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

随机推荐

  1. python数组(列表、元组及字典)

    python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素. 定义方式:arr = [元素] (2 ...

  2. STL:set用法总结

    一:介绍 set是STL的关联式容器,以红黑树(Red-Black Tree)作为底层数据结构.自动去重,保证每个元素唯一,并对数据进行排序. 命名空间为std,所属头文件为<set> 二 ...

  3. 负载均衡服务器中存在大量的TIME_WAIT怎么解决

    首先需要明白什么是TIME_WAIT.TIME_WAIT是在tcp断开连接时进行四次回收的时候,主动断开端在收到被动关闭端的FIN包并发送ACK包给被动关闭后进入的状态.这个状态默认情况下是2倍的MS ...

  4. ubuntu-14.10 输入法切换设置

    1. 设置---系统设置 2. 语言支持---添加或删除语言---汉语 3. 关闭,系统设置找到 文本输入 4. 可以添加.删除,以及切换方式

  5. Linux下Makefile的automake生成全攻略--转

    http://www.yesky.com/120/1865620.shtml 作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下 ...

  6. Nginx+PM2+Node.js最简单的配置

    一个最简单的反向代理配置方式 server { listen ; server_name www.luckybing.top; location / { proxy_pass http://127.0 ...

  7. node会话管理——cookie-parser

    cookie是由服务器发送给客户端(浏览器)的小量信息. 我们知道,平时上网时都是使用无状态的HTTP协议传输出数据,这意味着客户端与服务端在数据传送完成后就会中断连接.这时我们就需要一个一直保持会话 ...

  8. [Linq To Sql]解决join时的Collation冲突

    背景 现在两表 A:

  9. Session&Cookie 简介及使用

    Cookie cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 或其它语言来创建和取回 cookie ...

  10. 认识Spring AOP

    Spring AOP AOP是Aspect/'æspekt/ Oriented/ɔːrɪentɪd/ Programming的缩写,意为:面向切面编程. 是什么 通过预编译方式和运行期动态代理实现程序 ...