题目链接: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. JavaScript有趣的知识点

    JavaScript中总有一些有趣的小知识,而且又是很容易犯错的.我把我遇到的慢慢罗列一下,方便大家避坑 typeof(null)返回的结果是 object " "变成布尔类型为t ...

  2. 对象(Object)相关

    详情参考 1.对象的表示方法 js原生提供Object构造函数.js中所有的对象都是Object的实例. 定义一个对象最简单的就是var obj = {}; ES6属性和方法允许简写.对象的super ...

  3. ue/um-editor实现word图片复制

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...

  4. Vue(一)

    什么是Vue? 用于构建用户界面的渐进式框架

  5. 【概率论】2-2:独立事件(Independent Events)

    title: [概率论]2-2:独立事件(Independent Events) categories: Mathematic Probability keywords: Independent Ev ...

  6. Luogu5298 [PKUWC2018]Minimax

    太久没写博客了,过来水一发. 题目链接:洛谷 首先我们想到,考虑每个叶节点的权值为根节点权值的概率.首先要将叶节点权值离散化. 假设现在是$x$节点,令$f_i,g_i$分别表示左/右节点的权值$=i ...

  7. jenkins自动化部署gitlab上maven程序

    部署流程:将代码从gitlab上拉取下来,使用maven打包,将打包后的jar通过ssh发送到服务器上,运行jar程序 注意:本文需要安装一些插件Publish Over SSH 1.新建任务 在主页 ...

  8. ICEM-extrude功能画圆柱绕流网格【转载】

    转载自:http://blog.csdn.net/lgw19910426/article/details/26401517 首先画网格大体顺序为点-->线-->面-->单元体. 第一 ...

  9. ASP.NET的MVC设计模式

    当开发者听到“设计模式”这个词时,他们通常联想到两个场景.一组开发者正在讨论许多创造性意见,正在开会,但是却没有进行编码.另外一组人能制定出正确的计划,保证系统能够开发成功,代码可以重用. 而现实一般 ...

  10. java跨域配置

    一.问题 使用前后端分离模式开发项目时,往往会遇到这样一个问题 -- 无法跨域获取服务端数据 这是由于浏览器的同源策略导致的,目的是为了安全.在前后端分离开发模式备受青睐的今天,前端和后台项目往往会在 ...