hdu 1533 Going Home (KM)
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6641 Accepted Submission(s): 3491
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.
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.
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
2
10
28
C/C++:
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int my_max = , my_max_hm = ; int my_map[my_max][my_max], n, m, N, my_line[my_max_hm]
, my_lx[my_max_hm], my_ly[my_max_hm], my_slack[my_max_hm]
, my_bookx[my_max_hm], my_booky[my_max_hm]; struct node
{
int x, y;
} my_home[my_max_hm], my_men[my_max_hm]; bool my_dfs(int x)
{
my_bookx[x] = ;
for (int i = ; i <= N; ++ i)
{
if (my_booky[i]) continue;
int temp = my_lx[x] + my_ly[i] - my_map[x][i];
if (temp == )
{
my_booky[i] = ;
if (!my_line[i] || my_dfs(my_line[i]))
{
my_line[i] = x;
return true;
}
}
else if (my_slack[i] > temp)
my_slack[i] = temp;
}
return false;
} int my_km()
{
memset(my_line, , sizeof(my_line));
memset(my_ly, , sizeof(my_ly));
for (int i = ; i <= N; ++ i)
{
my_lx[i] = -INF;
for (int j = ; j <= N; ++ j)
if (my_lx[i] < my_map[i][j])
my_lx[i] = my_map[i][j];
} for (int i = ; i <= N; ++ i)
{
for (int j = ; j <= N; ++ j)
my_slack[j] = INF;
while ()
{
memset(my_bookx, , sizeof(my_bookx));
memset(my_booky, , sizeof(my_booky)); if (my_dfs(i)) break;
int my_temp_min = INF;
for (int j = ; j <= N; ++ j)
if (!my_booky[j] && my_slack[j] < my_temp_min)
my_temp_min = my_slack[j]; for (int j = ; j <= N; ++ j)
if (my_bookx[j]) my_lx[j] -= my_temp_min;
for (int j = ; j <= N; ++ j)
if (my_booky[j]) my_ly[j] += my_temp_min;
else my_slack[j] -= my_temp_min;
}
}
int my_ans = ;
for (int i = ; i <= N; ++ i)
my_ans += my_map[my_line[i]][i];
return my_ans;
} int main()
{
while (scanf("%d%d", &n, &m), n || m)
{
int my_cnt_h = , my_cnt_m = ;
memset(my_map, , sizeof(my_map));
getchar();
for (int i = ; i <= n; ++ i)
{
char my_s[my_max];
scanf("%s", my_s);
for (int j = ; j < m; ++ j)
{
if (my_s[j] == 'H')
{
my_home[my_cnt_h].x = i;
my_home[my_cnt_h].y = j;
my_cnt_h ++;
}
else if (my_s[j] == 'm')
{
my_men[my_cnt_m].x = i;
my_men[my_cnt_m].y = j;
my_cnt_m ++;
}
}
} N = my_cnt_h;
for (int i = ; i <= N; ++ i)
{
for (int j = ; j <= N; ++ j)
{
my_map[i][j] += abs(my_men[i - ].x - my_home[j - ].x);
my_map[i][j] += abs(my_men[i - ].y - my_home[j - ].y);
my_map[i][j] *= -;
}
}
printf("%d\n", - * my_km());
}
return ;
}
hdu 1533 Going Home (KM)的更多相关文章
- 【HDU 1533】 Going Home (KM)
Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, ...
- HDU 1533 Going Home(KM完美匹配)
HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- HDU 1533:Going Home(KM算法求二分图最小权匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description On a grid map there ...
- HDU 1533 & KM模板
题意 求二分图最小完备匹配. SOL 建个图那么方便的事情是吧...然后边权都是正的(好像根边权也没什么关系),既然要求最小那么把边权取个相反数跑个KM就好了.. CODE: /*========== ...
- HDU 1533 KM算法(权值最小的最佳匹配)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1533 KM或费用流
以前用KM写过,现在再用费用流写. #include <iostream> #include <cstdio> #include <cstring> #includ ...
- [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...
随机推荐
- .Net Core3.0 配置Configuration
准备 .NET core和.NET项目配置上有了很大的改变,支持的也更加丰富了比如命令行,环境变量,内存中.NET对象,设置文件等等..NET项目我们常常把配置信息放到webConfig 或者appC ...
- ggstatsplot绘图|统计+可视化,学术科研神器
本文首发于“生信补给站”公众号,https://mp.weixin.qq.com/s/zdSit97SOEpbnR18ARzixw 更多关于R语言,ggplot2绘图,生信分析的内容,敬请关注小号. ...
- Java 获取前一天的24小时
//获取凌晨时间 public static Date getTodayStartTime(){ Calendar todayEnd = Calendar.getInstance(); todayEn ...
- WordCount的实现和测试
WordCount 一.开头 (1)合作者:201631107110,201631083416 (2)代码地址:https://gitee.com/zhaoxiaoqin/WordCount.git ...
- HTML5+CSS:02用户注册表单
新的学期已开始接近两个月了,还记得是在国庆节那几天申请的博客账号,可过了一个月都还没开始写博客,(>_<)有点小偷懒了,不过,学习还是不能落下的,今写一个有点实践运用的关于 ...
- python dict(字典)
补充知识点1: 数据类型的划分:可变数据类型.不可变数据类型 可变数据类型: 元组,bool,int,str --可哈希 不可变数据类型: list,dict,set ...
- Excel的IYQ钓鱼
0x00 环境准备 1.操作系统:windows7 2.microsoft office版本:office 2010 0x01 了解IYQ的基本概念 可以将IYQ简单的理解成内置在excel中的一种特 ...
- C语言I作业06
问题 答案 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/9888 我在 ...
- MIT线性代数:12.图和网络
- TCP/IP协议指南
分组: packet 通常用来表示任何类型的报文. 数据报: datagram 表示网络分层技术,它也经常用于表示在OSI参考模型较高层上的发送报文. 帧: frame 特别常见用于数据链路层上的报文 ...