Going Home
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6641    Accepted Submission(s): 3491
Problem 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

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)的更多相关文章

  1. 【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, ...

  2. HDU 1533 Going Home(KM完美匹配)

    HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...

  3. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  4. HDU 1533:Going Home(KM算法求二分图最小权匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description   On a grid map there ...

  5. HDU 1533 & KM模板

    题意 求二分图最小完备匹配. SOL 建个图那么方便的事情是吧...然后边权都是正的(好像根边权也没什么关系),既然要求最小那么把边权取个相反数跑个KM就好了.. CODE: /*========== ...

  6. HDU 1533 KM算法(权值最小的最佳匹配)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. hdu 1533 KM或费用流

    以前用KM写过,现在再用费用流写. #include <iostream> #include <cstdio> #include <cstring> #includ ...

  8. [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. 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 ...

随机推荐

  1. Knative 实战:基于 Kafka 实现消息推送

    作者 | 元毅 阿里云智能事业群高级开发工程师 导读:当前在 Knative 中已经提供了对 Kafka 事件源的支持,那么如何基于 Kafka 实现消息推送呢?本文作者将以阿里云 Kafka 产品为 ...

  2. php函数分为哪两种?

    PHP的真正威力源自于它的函数.函数分为内置函数和自定义函数. 内置函数 所谓PHP内置函数,就是在php程序的库里面已经定义了的函数,比如echo,mysql_connect,include_onc ...

  3. Docker卷

    要解决的问题:在移除了现有容器或者添加了新容器时,之前容器的数据无法访问. 为避免上述问题,Docker提供了以下策略来持久化数据 tmpfs挂载 绑定挂载 卷 1.tmpfs挂载 2.绑定挂载 将D ...

  4. 玩转u8g2 OLED库,一篇就够(分篇)

    授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...

  5. 百万年薪python之路 -- 运算符及while的练习

    1.判断下列逻辑语句的结果,一定要自己先分析 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 1 &g ...

  6. 百万年薪python之路 -- python2和python3的区别

    python2和python3的区别: python2获取的是整数 python3获取的是浮点数 print函数:(Python3中print为一个函数,必须用括号括起来:Python2中print为 ...

  7. Airflow速用

    Airflow是Apache用python编写的,用到了 flask框架及相关插件,rabbitmq,celery等(windows不兼容):. 主要实现的功能 编写 定时任务,及任务间的编排: 提供 ...

  8. Unity Dropdown

    unity DropDown控件应用很简单 代码如下 frameDpdown.options.Clear(); //Dropdown.OptionData optDataFrame = new Dro ...

  9. 实战--dango自带的分页(极简)

    注意,我将templates定义在项目的同级目录下: 在settings.py中配置 TEMPLATES = [ { 'BACKEND': 'django.template.backends.djan ...

  10. axio安装及使用

    先安装 npm install axios --save 再导入 import $ from "jquery"; import axios from "axios&quo ...