POJ2195 Going Home【KM最小匹配】
题目链接:http://poj.org/problem?id=2195
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:27287 | Accepted: 13601 |
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
#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最小匹配】的更多相关文章
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- HDU 1533 Going Home(KM完美匹配)
HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...
- 【转载】【最短路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 ...
- perl学习之:理解贪婪匹配和最小匹配之间的区别
正则表达式的新手经常将贪婪匹配和最小匹配理解错误.默认情况下,Perl 的正则表达式是“贪婪地”,也就是说它们将尽可能多地匹配字符. 下面的脚本打印出“matched defgabcdef”,因为它尽 ...
- POJ2195 Going Home —— 最大权匹配 or 最小费用最大流
题目链接:https://vjudge.net/problem/POJ-2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total ...
- Q - Tour - hdu 3488(最小匹配值)
题意:一个王国有N个城市,M条路,都是有向的,现在可以去旅游,不过走的路只能是环(至少也需要有两个城市),他们保证这些城市之间的路径都是有环构成的,现在至少需要走多少路. 分析:因为是有向图所以,而且 ...
- POJ 2516 Minimum Cost(拆点+KM完备匹配)
题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...
- BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡:贪心【最小匹配代价】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3399 题意: 给你一个数列a,和一个可变换顺序的序列b(数列长度≤25000). a增加一 ...
- 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)
[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS Memory Limit ...
随机推荐
- C语言笔记 09_共用体&typedef&输入|输出
共用体 共用体允许您在相同的内存位置存储不同的数据类型.您可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值.共用体提供了一种使用相同的内存位置的有效方式. 定义共用体 为了定义共用体, ...
- 10 | MySQL为什么有时候会选错索引?
前面我们介绍过索引,你已经知道了在MySQL中一张表其实是可以支持多个索引的.但是,你写SQL语句的时候,并没有主动指定使用哪个索引.也就是说,使用哪个索引是由MySQL来确定的. 不知道你有没有碰到 ...
- JS 的 new 到底是干什么的?
大部分讲 new 的文章会从面向对象的思路讲起,但是我始终认为,在解释一个事物的时候,不应该引入另一个更复杂的事物. 今天我从「省代码」的角度来讲 new. --------------------- ...
- python 文件,文件夹,路径操作
判断路径或文件os.path.isabs(...) # 判断是否绝对路径os.path.exists(...) # 判断是否真实存在os.path.isdir(...) # 判断是否是个目录os.pa ...
- 学python必须知道的30个技巧
收集这些有用的捷径技巧 1. 原地进行交换两个数字 我们对赋值的右侧进行一个新的元组,左侧解析(unpack)那个(未被引用的)元组到变量 <a> 和 <b> 赋值完成时,新的 ...
- 用Ajax请求后台数据
我们先不讲ajax的原理,还是先以实战为主,看一下这个东西到底怎么用的? form表单: <%@ page language="java" contentType=" ...
- Springboot使用zuul进行负载均衡
完整项目代码地址参考:https://github.com/SimonHu1993/SpringbootZuul 1.这里我们使用Eureka来作为服务的注册与发现中心,首先看看Eureka clie ...
- mongodb 数据更新命令、操作符
一.Mongodb数据更新命令 Mongodb更新有两个命令:update.save. 1.1update命令 update命令格式: db.collection.update(criteria,ob ...
- CDH构建大数据平台-Kerberos高可用部署【完结篇】
CDH构建大数据平台-Kerberos高可用部署[完结篇] 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装Kerberos相关的软件包并同步配置文件 1>.实验环境 ...
- Rect和RectF函数
1.是否包含点,矩形 判断是否包含某个点 boolean contains(int x,int y) 函数用于判断某个点是否在当前矩形中,如果在,则返回true ,不在则false 2.判断是否包含 ...