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. jquery的返回顶端的功能实现

    页面很长的时候,读到最下面,需要返回顶端,则在页面最下面布局一个返回顶部的图标很有用. 具体功能是,jquey控制,向下滚动出现返回顶部图片,若滚动返回顶部或点回顶部,则图标消失. 实现效果如下图:

  2. 新手也能看懂的 SpringBoot 异步编程指南

    本文已经收录自 springboot-guide : https://github.com/Snailclimb/springboot-guide (Spring Boot 核心知识点整理. 基于 S ...

  3. MacOS访达增强工具-TotalFinder

    TotalFinder 是Mac上最好用的Finder增强工具,TotalFinder 提供了多标签式浏览.拷贝路径.剪切文件.显示隐藏文件.双栏窗口模式.彩色标签等功能 彩色的标签 将彩色带回El ...

  4. 【MySQL】事务隔离级别及ACID

    注:begin或start transaction并不是一个事务的起点,而是在执行它们之后的第一个操作InnoDB表的语句,事务才真正开始.start transaction with consist ...

  5. 实战SpringCloud响应式微服务系列教程(第八章)构建响应式RESTful服务

    本文为实战SpringCloud响应式微服务系列教程第八章,讲解构建响应式RESTful服务.建议没有之前基础的童鞋,先看之前的章节,章节目录放在文末. 1.使用springboot2.1.4构建RE ...

  6. JS移动端适配(自适应)

    var html = document.querySelector('html'); changeRem(); window.addEventListener('resize', changeRem) ...

  7. Knative 实战:如何在 Knative 中配置自定义域名及路由规则

    作者 | 元毅 阿里云智能事业群高级开发工程师 当前 Knative 中默认支持是基于域名的转发,可以通过域名模板配置后缀,但目前对于用户来说并不能指定全域名设置.另外一个问题就是基于 Path 和 ...

  8. 思科Cisco 交换机 VTP负载均衡的配置

    思科Cisco 交换机 VTP负载均衡的配置 3560三层交换机配置: int ran fa0/23 - fa0/24 sw trunk encapsolution dot1q sw mode tru ...

  9. 设计模式(六)Prototype模式

    Prototype模式就是不根据类来生成实例,而是根据实例来生成新实例.至于为什么不能根据类来生成实例,在最后会讲到. 还是根据实例程序来理解这种设计模式吧. 下面是实例代码. package Big ...

  10. SpringCloud之链路追踪整合Sleuth(十三)

    前言 SpringCloud 是微服务中的翘楚,最佳的落地方案. 在一个完整的微服务架构项目中,服务之间的调用是很复杂的,当其中某一个服务出现了问题或者访问超时,很 难直接确定是由哪个服务引起的,所以 ...