POJ 2195 Going Home 【二分图最小权值匹配】

KM算法 最优匹配(最大权匹配) hdu 2255 奔小康赚大钱 最小权匹配 poj 2195 Going Home 最大权二分匹配问题就是给二分图的每条边一个权值,选择若干不相交的边,得到的总权值最大。解决这个问题可以用KM算法。理解KM算法需要首先理解“可行顶标”的概念。可行顶标是指关于二分图两边的每个点的一个值lx[i]或ly[j],保证对于每条边w[i][j]都有lx[i]+ly[j]-w[i][j]>=0。如果所有满足lx[i]+ly[j]==w[i][j]的边组成的导出子图中存在一个完美匹配,那么这个 阅读详情

传送门: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:

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <cstring>
  5 #define INF 0x3f3f3f3f
  6 using namespace std;
  7 const int MAXN = 110;
  8 
  9 char str[MAXN][MAXN];
 10 int c[MAXN][MAXN];
 11 int ex[MAXN], ey[MAXN];
 12 bool visx[MAXN], visy[MAXN];
 13 int match[MAXN];
 14 int HH, WW;
 15 int numx, numy;
 16 
 17 struct date
 18 {
 19     int x, y;
 20 }M[MAXN], H[MAXN];
 21 
 22 bool dfs(int x)
 23 {
 24     visx[x] = true;
 25 
 26     for(int y = 0; y < numx; y++){
 27         if(!visy[y] && ex[x]+ey[y]-c[x][y] == 0){
 28             visy[y] = true;
 29 
 30             if(match[y] == -1 || dfs(match[y])){
 31                 match[y] = x;
 32                 return true;
 33             }
 34         }
 35     }
 36     return false;
 37 }
 38 
 39 void KM()
 40 {
 41     memset(ey, 0, sizeof(ey));
 42     memset(match, -1, sizeof(match));
 43 
 44     for(int i = 0; i < numx; i++){
 45         ex[i] = c[i][0];
 46         for(int j = 1; j < numx; j++){
 47             if(c[i][j] > ex[i])
 48                 ex[i] = c[i][j];
 49         }
 50     }
 51 
 52     for(int i = 0; i < numx; i++){
 53         while(1){
 54             memset(visx, 0, sizeof(visx));
 55             memset(visy, 0, sizeof(visy));
 56 
 57             if(dfs(i)) break;
 58 
 59             int d = INF;
 60             for(int j = 0; j < numx; j++){
 61                 if(visx[j]){
 62                     for(int k = 0; k < numx; k++){
 63                         if(!visy[k] && ex[j]+ey[k]-c[j][k] < d)
 64                             d = ex[j] + ey[k] - c[j][k];
 65                     }
 66                 }
 67             }
 68 
 69             for(int j = 0; j < numx; j++){
 70                 if(visx[j]) ex[j]-=d;
 71                 if(visy[j]) ey[j]+=d;
 72             }
 73         }
 74     }
 75 
 76     int res = 0;
 77     for(int i = 0; i <numx; i++){
 78         res+=c[match[i]][i];
 79     }
 80     printf("%d\n", -res);
 81 }
 82 
 83 int main()
 84 {
 85     while(~scanf("%d %d", &HH, &WW) && (HH+WW)){
 86         numx = numy = 0;
 87         for(int i = 0; i < HH; i++){
 88             scanf("%s", &str[i]);
 89 
 90             for(int j = 0; j < WW; j++){
 91                 if(str[i][j] == 'm'){
 92                     M[numx].x = i;
 93                     M[numx++].y = j;
 94                 }
 95                 else if(str[i][j] == 'H'){
 96                     H[numy].x = i;
 97                     H[numy++].y = j;
 98                 }
 99             }
100         }
101 
102         for(int i = 0; i < numx; i++){
103             for(int j = 0; j < numx; j++)
104                 c[i][j] = -(abs(M[i].x - H[j].x) + abs(M[i].y - H[j].y));
105         }
106         KM();
107     }
108     return 0;
109 }
View Code

 

Claude Code插件安装与VSCode深度集成实战指南 Claude Code是一款专为IDE设计的AI编程助手插件,其核心价值在于将自然语言意图转化为可运行代码,从而优化开发者认知带宽分配。它依赖Node.js运行时、git版本上下文和VSCode编辑器API三者协同工作,技术原理涉及本地进程通信、权限策略绕过、上下文感知补全等关键机制。该工具显著提升工程效率,适用于CLI开发、EXIF元数据处理、异步逻辑生成等典型JavaScript/TypeScript应用场景。本文聚焦Claude Code插件在VSCode中的真实落地链路,覆盖Windows/macO 阅读详情

相关推荐

RK3588网络调试笔记:当我把默认RTL8211F PHY换成另一款时,踩了哪些坑?

本文详细记录了在RK3588平台上将默认RTL8211F PHY芯片替换为其他型号的全过程,包括硬件设计验证、内核驱动适配、复位时序配置等关键步骤。通过实际案例分享,提供了从驱动调试到硬件优化的系统性解决方案,帮助开发者高效完成PHY芯片替换,确保网络接口稳定性。

weixin_30291791的博客 669

poj2195——最小费用最大流模板题

题目链接:http://poj.org/problem?id=2195 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 p...

mumei的博客 330

推荐系统:NDCG评价指标及其Python实现方法

除了推荐任务,在二分类任务中,也可以计算该指标,代码如下: # ndcg def get_dcg(y_pred, y_true, k): #注意y_pred与y_true必须是一一对应的,并且y_pred越大越接近label=1(用相关性的说法就是,与label=1越相关) df = pd.DataFrame({"y_pred":y_pred, "y_true":y_true}) ...

lanyuelvyun的博客 8242

【KM算法】B - Going Home POJ - 2195

https://vjudge.net/contest/389071#problem/B 题目描述 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

wldcmzy的博客 267

DPARSF高级版 vs 基础版怎么选?没有T1数据也能做fMRI分析吗?

本文深度对比了DPARSF高级版与基础版在fMRI分析中的核心差异与适用场景,特别针对无T1数据的情况提供了实战解决方案。高级版依赖T1像实现高精度配准,适合需要精确解剖定位的研究;基础版则无需T1数据,适用于功能连接等相对空间要求不高的分析。文章还提供了版本选择决策树和常见问题解决方案,帮助研究者根据实际需求做出最优选择。

weixin_42526087的博客 132

华为数通---配置风暴控制案例

风暴控制可以通过阻塞报文或关闭端口来阻断广播、未知组播和未知单播报文的流量。

2301_77081516的博客 1626

Going Home (最小权值完备匹配)

传送门HDU1533描述 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 ad...

测试 492

路径搜索算法——RRT*算法

RRT算法是RRT算法的优化版本,通过引入路径优化机制实现渐进最优性。该算法在高维空间和复杂环境中表现优异,主要步骤包括随机采样、树扩展和邻近节点重新连接。相比RRT,RRT能够不断优化路径质量,但计算复杂度更高。Python实现展示了其核心逻辑:随机采样扩展树节点,检查碰撞,选择最优父节点,并通过重新连接优化路径。算法适用于机器人导航等场景,但需平衡计算效率与路径质量。

swaggytiger的博客 3062

POJ 2195 Going Home (最小费用最大流)

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 ma

小坏蛋_千千 719

风电功率预测项目技术白皮书:短期、超短期、中长期全尺度算法详解与工程落地

聚焦风电功率预测的时间尺度与算法:短期(0~72h)以XGBoost/LightGBM/CatBoost及深度学习为主,超短期(0~4h)采用ARMA+LSTM,中长期用LightGBM等,并涵盖概率预测、多气象源融合、AI大模型+物理模型混合策略、极端天气应对及工程运维要点。

算法工程师 326

YOLOv8改进策略【Conv和Transformer】| CVPR-2023 BiFormer 稀疏自注意力,减少内存占用

本文记录的是利用双级路由注意力机制优化的目标检测网络模型。传统的复杂度高,随着输入空间分辨率增加,计算量呈平方增长,导致严重的可扩展性问题。而本文所使用的在获取全局信息的同时,通过区域到区域路由和令牌到令牌注意力的合理设计,减少了不必要的计算量。BiFormer: Vision Transformer with Bi-Level Routing Attention采用四阶段金字塔结构,与一些先进的视觉Transformer类似。在每个阶段,首先使用重叠的补丁嵌入(在第一阶段)或补丁合并模块(在第二到第四阶段

Limiiiing的博客 614

POJ2195: Going Home 题解

这道题是费用流模板题 处理费用流可以用edmonds-karp+spfa算法,这样可以有效的处理负边,但spfa的复杂度不够稳定 我们可以导入势的概念从而使用dijkstra+edmonds-karp来解决费用流 设顶点i的势为h(i),我们将图中原来的边d(e)变成d'(e)=d(e)+h(u)-h(v),这样改动后的图最终的答案只要减去h(s)-h(t)就是原图的答案 所以只要合理的选

IcePrincess_1968's World 353

STM32 IIC温湿度传感器(STH45)实验

精度 ΔRH = ±1.0 %RH, ΔT = ±0.1 °C• 平均电流:0.4 μA,空闲电流:80 nA• I2C FM、CRC 校验和、乘法。I2C 地址。• 专利保护选项 [1]、PTFE 膜和可拆卸保护盖• 工作范围:0 …100 %相对湿度,−40…125 摄氏度• 在冷凝环境中功能齐全• 动力加热器,真正的NIST可追溯性• JEDEC JESD47 认证 • 符合 ISO 17025 : 2017 的传感器专用校准证书,3 点温度校准。

baidu_34971492的博客 3075

poj2195 Going Home(二分图的最小权匹配)

//二分图的最小权匹配 //将权值以及顶标初始化为负,最后取反就好 #include #include #include #include using namespace std; #define clr(a) memset(a,0,sizeof(a)); const int N=300; const int INF=0x3fffffff; int G[204][204]; struct KM{

野生的TFgirl 469

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

POJ 2195 Going Home(二分图最优匹配)

POJ 2195 Going Home(二分图最优匹配) http://poj.org/problem?id=2195 题意:        给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致。man每移动一格需花费$1(即单位费用=单位距离),一间house只能入住一个man。现在要求所有的man都入住house,求最小费用。 分析:

code 1194

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

POJ - 2195 Going Home (构图 最大匹配KM算法)

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

Deadpool 365

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

poj 2195 Going Home 二分图最小权匹配KM算法

KM算法解二分图最大(小)权匹配的简洁准确模板!(配例题)

sepNINE的专栏 1105

HDU 1533 && poj 2195 Going Home KM算法

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

弱智就要多努力 561

POJ2195 Going Home【二分图最佳匹配】

题目大意: 在一个N*M的矩阵中,有M个人和M个房子,每个人要安排一个房子,每个房子只能安排一个人。 而每个人移动一步需要一美元。那么问题来了:求为每个人安排房子移动所需要的金钱最小值是多 少。 思路: 做一个二分图,一边为人,另一边为房子,如果把人和房子之间的距离作为边权的话,问题就变成 了求带权二分图最小权和的最佳匹配。这里我们为了方便计算,吧人和房子之间的距离的负值作为 边权,那么就变成了求带权二分图最大权和的最佳匹配,就是经典的二分图最佳匹配问题。用KM算 法解出最大权值和。取其相反,就得到了最小

程序员充电站(itcharge) 1118

POJ 2195 Going Home 二分图的最大权匹配

POJ2195 Going Home 最小费用最大流 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25567   Accepted: 12838 Description On a grid map there are n little men and n hou...

紫芝的博客 502

poj2195 Going Home (二分图最小权匹配)

<br />Going Home<br /><br /><br />可以转成最大权匹配,直接套模板……<br />#include <iostream> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <string> #include <cstdio> #include <climits> #include <queue> #include <map> #inclu

281

POJ 2195 - Going Home(二分图最大权匹配)

题目: http://poj.org/problem?id=2195 题意: N个人N个房子,每个人到房间都会有一定的距离,要求所有的人都回到房间中,求出最小的总距离。 思路: 二分图最大匹配模板。 将每个人到所有的房间建边,边的权值为人到房子的距离,因为是求最小权值则将边的权值取反。最后答案取反即可。 AC. #include #include #include #in

Halo 573

【POJ 2195】 Going Home(KM算法求最小权匹配)

【POJ 2195】 Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20303   Accepted: 10297 Description On a grid map there a

ACMer' 2477

POJ 2195&&HDU 1533 Going Home(KM算法解决二分图最小权匹配)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5777    Accepted Submission(s): 3060 Problem Description On a grid map ther

Yuki_fx的博客 605

poj 2195 Going Home (KM算法)

呃,省赛已经过去了,生活应该继续~~ 好几天没写博了,一直在研究KM算法,今天算是有点小明白了,做了一道模板题练练手。 先讲讲我理解的KM算法吧,如果你已经学会二分匹配中的匈牙利算法,那么要理解KM算法就很容易了,其实KM算法就是在匈牙利算法的基础上加上两点的权值。 首先是相等子图的概念:设顶点Xi的顶标为A[ i ],顶点Yj的顶标为B[ j ],顶点Xi与Yj之间的边权为w...

B218707的博客 199
上一篇: 二分图最大权值匹配 KM算法 模板
下一篇: POJ 3565 Ants 【最小权值匹配应用】
莜莫oO
博客等级 码龄9年 7粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值