题目链接:http://poj.org/problem?id=2195

Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:27287   Accepted: 13601

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.
 
 题意:在图上有相同数量的人和房子,人走一步的代价为1,求每个人都进入房子后代价和最小为多少。
 思路:
 1.利用KM算法求最小匹配,将人作为二分图的x部的点,房子作为y部的点,边权为走一步的代价*哈曼顿距离。需要注意的是,KM算法是求最大匹配的,求最小匹配需要将边权取负值,初始化lx[]数组时需要取 -inf,最后返回答案也要返回相反值
 2.用最小费用最大流的做法在这里 https://www.cnblogs.com/yuanweidao/p/11254863.html
 代码如下:
 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define mem(a, b) memset(a, b, sizeof(a))
const int inf = 0x3f3f3f3f;
using namespace std; int n, m;
char map[][];
int lx[], ly[], match[], visx[], visy[], weight[][], slack[]; struct Node
{
int x, y;
}xx[], yy[];
int cnt1, cnt2; int find(int x)
{
visx[x] = ;
for(int j = ; j <= cnt2; j ++)
{
if(!visy[j])
{
int t = lx[x] + ly[j] - weight[x][j];
if(t == )
{
visy[j] = ;
if(match[j] == - || find(match[j]))
{
match[j] = x;
return ;
}
}
else if(slack[j] > t)
slack[j] = t;
}
}
return ;
} int KM()
{
mem(lx, -inf); //最小权 lx初始化为 -inf
mem(ly, ), mem(match, -);
for(int i = ; i <= cnt1; i ++)
for(int j = ; j <= cnt2; j ++)
lx[i] = max(lx[i], weight[i][j]);
for(int i = ; i <= cnt1; i ++)
{
for(int j = ; j <= cnt2; j ++)
slack[j] = inf;
while()
{
mem(visx, ), mem(visy, );
if(find(i))
break;
int d = inf;
for(int j = ; j <= cnt2; j ++)
if(!visy[j] && d > slack[j])
d = slack[j];
for(int j = ; j <= cnt2; j ++)
{
if(!visy[j])
slack[j] -= d;
else
ly[j] += d;
}
for(int j = ; j <= cnt1; j ++)
if(visx[j])
lx[j] -= d;
}
}
int ans = ;
for(int j = ; j <= cnt2; j ++)
if(match[j] != -)
ans += weight[match[j]][j];
return -ans;//返回负值
} int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == && m == )
break;
getchar();
cnt1 = , cnt2 = ;
for(int i = ; i <= n; i ++)
scanf("%s", map[i] + );
for(int i = ; i <= n; i ++)
for(int j = ; j <= m; j ++)
{
if(map[i][j] == 'm')//存人的点
xx[++ cnt1].x = i, xx[cnt1].y = j;
else if(map[i][j] == 'H')//存房子的点
yy[++ cnt2].x = i, yy[cnt2].y = j;
}
for(int i = ; i <= cnt1; i ++) //KM求最小匹配 边权赋为 负值
for(int j = ; j <= cnt2; j ++)
weight[i][j] = -(abs(xx[i].x - yy[j].x) + abs(xx[i].y - yy[j].y));
int ans = KM();
printf("%d\n", ans);
}
return ;
}

KM算法求最小匹配

 
 

POJ2195 Going Home【KM最小匹配】的更多相关文章

  1. hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  2. HDU 1533 Going Home(KM完美匹配)

    HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...

  3. 【转载】【最短路Floyd+KM 最佳匹配】hdu 2448 Mining Station on the Sea

    Mining Station on the Sea Problem Description The ocean is a treasure house of resources and the dev ...

  4. perl学习之:理解贪婪匹配和最小匹配之间的区别

    正则表达式的新手经常将贪婪匹配和最小匹配理解错误.默认情况下,Perl 的正则表达式是“贪婪地”,也就是说它们将尽可能多地匹配字符. 下面的脚本打印出“matched defgabcdef”,因为它尽 ...

  5. POJ2195 Going Home —— 最大权匹配 or 最小费用最大流

    题目链接:https://vjudge.net/problem/POJ-2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total ...

  6. Q - Tour - hdu 3488(最小匹配值)

    题意:一个王国有N个城市,M条路,都是有向的,现在可以去旅游,不过走的路只能是环(至少也需要有两个城市),他们保证这些城市之间的路径都是有环构成的,现在至少需要走多少路. 分析:因为是有向图所以,而且 ...

  7. POJ 2516 Minimum Cost(拆点+KM完备匹配)

    题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...

  8. BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡:贪心【最小匹配代价】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3399 题意: 给你一个数列a,和一个可变换顺序的序列b(数列长度≤25000). a增加一 ...

  9. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

随机推荐

  1. Tpcc-mysql 结果解读

    原文:https://blog.csdn.net/frockee/article/details/87812329 1. 填坑经验   不要使用tidb的tpcc测试程序(非标准,tidb修改过),使 ...

  2. 正则regex

    Regual expression 普通正常字符 字符匹配 . 表示任意字符 匹配次数 位置锚定 分组及引用

  3. BSGS ! x

    一.引入: 若存在一个式子a^b ≡ c (mod p) (p ≡ 1000000007,且0<a,b,c<p) 已知a,b,求c.  这不就是快速幂嘛! 已知a,c,求b. 这就是我们需 ...

  4. wait系列

    转自 http://blog.csdn.net/todd911/article/details/15028511 1.wait函数和waitpid函数 当一个进程正常或异常终止时,内核就向其父进程发送 ...

  5. python的协程,monkeyPatch

    monkey patch 一般指运行时候进行动态替换. 基本上我们使用gevent,会在最开头的地方加入gevent.monkey.patch_all();把标准库中的thread/socket等给替 ...

  6. MySQL数据分析实战-朱元禄-专题视频课程

    MySQL数据分析实战-496人已学习 课程介绍        本套课程由知名数据分析博主jacky老师录制,深入浅出讲解MySQL数据分析,从实战角度出发,帮助大家制胜职场!课程收益    1.学会 ...

  7. 第一章:Python数据分析前的基础铺垫

    本节概要 - 数据类型 - 数据结构 - 数据的常用操作方法 数据类型 基础铺垫 定义 我们搞数据时,首先要告诉Python我们的数据类型是什么 数值型:直接写一个数字即可 逻辑型:True,Fals ...

  8. 图解Linux进程间通信实现原理(1)

    为Linux应用程序的开发人员,对Linux的进程间通信方式肯定是了如指掌,平时的开发中应该会大量的使用到.当你迅速的在键盘上按下[CTRL+C]终止掉一个正在运行中的命令时,你有没有仔细的思考过背后 ...

  9. 小程序开发:用原生还是选框架(wepy/mpvue/uni-app/taro)?

    小程序开发:用原生还是选框架(wepy/mpvue/uni-app/taro)? 自 2017-1-9微信小程序诞生以来,历经2年多的迭代升级,已有数百万小程序上线,成为继Web.iOS.Androi ...

  10. Matlab下imwrite,Uint16的深度图像

    Matlab下imwrite,Uint16的深度图像 1. 在Matlab命令窗口输入命令: help imwrite 会有如下解释: If the input array is of class u ...