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 ...
随机推荐
- Express框架的整体感知
Express是基于node.js平台的快速.开放.极简的web开放框架,它的地位与作用有点类似于前端的jquery框架.它的英文官网地址为 http://expressjs.com,其对应的中文官网 ...
- LeetCode初级算法--树01:二叉树的最大深度
LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...
- Ubuntu中用户名密码和root密码修改
用户名密码和root密码不是同一个密码 重置(修改)root密码 ubuntu的root初始密码是随机的,每次开机都有一个新的root密码修改方法如下: 1.sudo passwd root 2.此处 ...
- Docker卷
要解决的问题:在移除了现有容器或者添加了新容器时,之前容器的数据无法访问. 为避免上述问题,Docker提供了以下策略来持久化数据 tmpfs挂载 绑定挂载 卷 1.tmpfs挂载 2.绑定挂载 将D ...
- 05jmeter-responses中有多组json数据,提取同一条json数据的两个字段
某接口返回结果如上图,取id和groupNo 1.读取确定的某条json数据:如取第一条,一个JSON Extractor即可实现 2.随机读取某条json数据: 需要两个JSON Extractor ...
- HTTP协议详解(一)——初识HTTP及请求
何为HTTP(超文本传输协议)? 协议,即约定俗成的规范.HTTP则是浏览器与服务器之间交流的一种规范.HTTP基于TCP/IP协议,属于应用层的协议. 为什么Web应用不使用TCP或者UDP协议呢? ...
- ASP.NET WebApi+Vue前后端分离之允许启用跨域请求
前言: 这段时间接手了一个新需求,将一个ASP.NET MVC项目改成前后端分离项目.前端使用Vue,后端则是使用ASP.NET WebApi.在搭建完成前后端框架后,进行接口测试时发现了一个前后端分 ...
- The usage of Markdown---链接的使用
目录 1. 序言 2. 网页链接 3. 图片链接 4. 页内跳转 更新时间:2019.09.14 1. 序言 在编辑文章的时候,我们常常需要插入各种链接,比如说网页链接,图片链接等等.当文章篇幅过 ...
- day10作业(函数实现注册''')
在猜年龄的基础上编写登录.注册方法,并且把猜年龄游戏分函数处理,如 登录函数 注册函数 猜年龄函数 选择奖品函数 '''在猜年龄的基础上编写登录.注册方法,并且把猜年龄游戏分函数处理,如 2. 登录函 ...
- OptimalSolution(2)--二叉树问题(4)子树与拓扑结构
一.判断t1树是否包含t2树全部的拓扑结构 1 / \ 2 3 2 / \ / \ / \ 4 5 6 7 4 5 / \ / / 8 9 10 8 返回:true 解法(O(M×N)):如果t1中某 ...