传送门: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. linux 卡在进度条进不去解决办法之一

    centos为例 一, 如下: 如果这个地方卡住了的话也许是你上次改了passwd文件,这个是其中一个情况. 如果刚刚开机就卡住了或者怎么卡住了的话在开机的读条时候摁esc显示读取的进程,根据显示的错 ...

  2. Java面试题搜集

    这里是一些Java面试题,从"程序员小灰"公众号转载过来,备用. 项目介绍 明确项目是做什么的 明确项目的价值.(为什么做这个项目,它解决了用户什么痛点,它带来什么价值?) 明确项 ...

  3. python读取excel表格生成sql语句 第一版

    由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦  作为程序猿当然要想办法解决,用Python写一个程序解决 需要用到 xlrd li ...

  4. 吴恩达《Machine Learning Yearning》总结(1-10章)

    1.为什么选择机器学习策略 案例:建立猫咪图像识别app 系统的优化可以有很多的方向: (1)获取更多的数据集,即更多的图片: (2)收集更多多样数据,如处于不常见的位置的猫的图,颜色奇异的猫的照片等 ...

  5. DW网页制作,数学,数据库管理

    数学(函数关系的建立) 函数关系:确定性现象之间的关系常常表现为函数关系,即一种现象的数量确定以后,另一种现象的数量也随之完全确定,表现为一种严格的函数关系. 如:记为y=f(x),其中x称为自变量, ...

  6. jdbc封装DBUtil

    1.编写实体类User public class User { private Integer id; private String username; private Integer age; pr ...

  7. App调用safar

    /调用safar打开网页 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.cnblo ...

  8. mybatis一对多映射

    场景: A:SecControlRulePojo.java B:SecControlSubRulePojo C:SecControlSubRuleManyPojo 实体A中包含List<B> ...

  9. Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互

    1 解析 1.1 这两个注解应用在什么地方 1.2 应用方式 1.3 xml方式实现取值 2 代码演练 2.1 @ImportResource和@Value代码演练 1 解析 1.1 这两个注解应用在 ...

  10. 用户选择wordpress程序建站需要知道的一些事情 - 安全、优化速度、配置

    WordPress是我们使用最多的CMS程序之一,无论是我们的个人博客,还是企业网站,甚至中小型站点,我们都可以用WP程序部署.我们看到海外网站的时候,大部分都是用的WORDPRESS程序.在我们国人 ...