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 ...
随机推荐
- MySQL 中Redo与Binlog顺序一致性问题
首先,我们知道在MySQL中,二进制日志是server层的,主要用来做主从复制和即时点恢复时使用的.而事务日志(redo log)是InnoDB存储引擎层的,用来保证事务安全的.现在我们来讨论一下My ...
- [Luogu] 借教室
https://www.luogu.org/problemnew/show/P1083 二分第i天不满足 前缀和 + 差分判断 #include <iostream> #include & ...
- 部署web前端的react项目到linux服务器
部署web前端的react项目到linux服务器 项目的目录结构 ``` ├─dlls #dlls编译后的问题 ├─doc #帮助文件入口 │ ├─src │ ├─apps #各个功能模块放在这里 │ ...
- 5 款最酷的 Linux 终端模拟器
转载:https://cloud.tencent.com/developer/article/1040344 首先我要推荐的第一个终端是 Xiki. Xiki 是 Craig Muth 的智慧结晶,他 ...
- 为ubuntu安装powerline记录
C:\Users\win10的用户名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\L ...
- python3 django项目从项目中导出依赖包
1. 在项目的根目录中使用mac终端执行命令, pip3 freeze > requirements.txt #requirements.txt只是个名字可以随便起,一般默认为requireme ...
- [转]用代码访问 Https
可以参考: https://blog.csdn.net/irokay/article/details/78801307 跳过证书验证方法 HttpClient简介HTTP 协议可能是现在 Intern ...
- 以太坊Geth通过私钥导入新地址到钱包步骤(3种方法)
一: 通过Geth客户端导入私钥: Open TextEdit Paste key into TextEdit without any extra characters or quotations S ...
- mysql密码设置为空怎么办?
由于很多童鞋安装使用MySQL时,安装时没有设置密码,或者像我一样图省事设置密码为空,想为其设置新密码: 1.点击 开始------>运行----在弹出的对话框中输入cmd 如下图: 2.使用 ...
- js中string和json的相互转换
1.将string转成json var json={}; var myorderno=$("#myorderno").val(); json.myorderno=myorderno ...