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 ...
随机推荐
- Vue框架构造
Vue 程序结构框架 Vue.js是典型的MVVM框架,什么是MVVM框架,介绍之前我们先介绍下什么是MVC框架 MVC 即 Model-View-Controller 的缩写,就是 模型-视图-控制 ...
- Github 高级搜索功能
参考文章链接:https://zhuanlan.zhihu.com/p/55294261 GitHub 提供高级搜索方式. 一.明确搜索仓库标题.仓库描述.README 1.只想查找仓库名称包含XX的 ...
- Linux power supply class(1)_软件架构及API汇整【转】
1. 前言 power supply class为编写供电设备(power supply,后面简称PSY)的驱动提供了统一的框架,功能包括: 1)抽象PSY设备的共性,向用户空间提供统一的API. 2 ...
- WPF实现放大镜
这是一个之前遗留的问题.wpf里面有很多很多的东西,我以前用的真的只是其中很小的一个角落都不到. 需求背景:图片来源于相机拍摄,由于对像素要求,拍出来的图像素比较高,原图尺寸为30722048,以目前 ...
- python学习-多线程并发
1.线程与进程 通俗解释: 对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进 ...
- 5. Sersync实时同步
rsync+Sersync数据的实时同步 sersync介绍 1.什么是实时同步 监控一个目录的变化, 当该目录触发事件(创建\删除\修改) 就执行动作, 这个动作可以是 rsync同步 ,也可以是其 ...
- 06 Node.js学习笔记之自动路由
在以往客户端请求的文件,我们都得判断匹配才能返回相应的数据,其实我们可以设置一个自动路由,就可以不用每次去判断用户访问的是那个文件了 //1.载入http和fs模块 var http=require( ...
- 三维动画形变算法(Mixed Finite Elements)
混合有限元方法通入引入辅助变量后可以将高阶微分问题变成一系列低阶微分问题的组合.在三维网格形变问题中,我们考虑如下泛函极值问题: 其中u: Ω0 → R3是变形体的空间坐标,上述泛函极值问题对应的欧拉 ...
- LIGHTX-CMS —— 基于 Node.js,Express.js 以及 SQLite 3 搭建的个人博客系统
概述 LIGHTX-CMS 是我基于 Node.js,Express.js 以及 SQLite 3 搭建的个人博客发布系统. 项目本身可以拿来部署个人博客网站,同时我认为其也适合用以新手学习 Node ...
- char 、signed char、unsigned char
看如下代码: char c = -1; signed char sc = -1; unsigned char uc = -1; printf("c=%d, sc=%d, uc=%d, cx= ...