题目链接

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

AC

  • 建边

    1. 源点到people建边,流量为1, 费用为0
    2. 所有的people到所有的house建边, 流量为1, 费用是曼哈顿距离
    3. house到汇点建边,流量为1,费用为0
  • 费用流模板

#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cmath>
#define N 80005
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;
// 本来是prev记录前驱节点的标号,但是hdu不让用,这里改为fuck
int fuck[10200], dis[10200], head[10200] ;
bool vis[10200];
int n, m, cnt;
int inf = 0x3f3f3f3f;struct ac{int v, c, cost, pre;
}edge[N];void addedge(int u, int v, int c, int cost) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].cost = cost;edge[cnt].pre = head[u];head[u] = cnt++;swap(u, v);edge[cnt].v = v;edge[cnt].c = 0;edge[cnt].cost = -cost;edge[cnt].pre = head[u];head[u] = cnt++;
}
bool spfa(int s, int e) {memset(vis, false, sizeof(vis));memset(dis, inf, sizeof(dis));memset(fuck, -1, sizeof(fuck));queue<int> que;que.push(s);dis[s] = 0;vis[s] = true;while (!que.empty()) {int u = que.front();que.pop();vis[u] = false;for (int i = head[u]; i != -1; i = edge[i].pre) {int v = edge[i].v;int c = edge[i].c;int cost = edge[i].cost;if (dis[v] > dis[u] + cost && c > 0) {dis[v] = dis[u] + cost;fuck[v] = i;if (!vis[v]) {vis[v] = true;que.push(v);}}}}if (dis[e] == inf)return false;elsereturn true;
}int mincost(int s, int e, int &cost) {int maxflow = 0;int minflow = inf;while (spfa(s, e)) {for (int i = fuck[e]; i != -1; i = fuck[edge[i ^ 1].v])minflow = min(minflow, edge[i].c);for (int i = fuck[e]; i != -1; i = fuck[edge[i ^ 1].v]) {edge[i].c -= minflow;edge[i ^ 1].c += minflow;cost += minflow * edge[i].cost;}maxflow += minflow;}return maxflow;
}
// 存放hose, people信息
struct cell {int num, x, y;
};
void debug(int a, int b, int c, int cost) {
//  cout << a << " -> " << b <<  " " << c << " " << cost << endl;
}
char g[102][102];
int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifwhile (scanf("%d%d", &n, &m), n + m) {memset(head, -1, sizeof(head));cnt = 0;int start = 0, end = n * m + 1;for (int i = 0; i < n; ++i) {scanf("%s", g[i]);}// 统计 people和house 的信息 vector<cell> house, people;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (g[i][j] == '.') continue;if (g[i][j] == 'H') {int num = i * m + j + 1;people.push_back((cell){num, i, j});}if (g[i][j] == 'm') {int num = i * m + j + 1;house.push_back((cell){num, i, j});}}}int house_num = house.size();int people_num = people.size();// 源点到people建边,花费0, 流量1 for (int i = 0; i < people_num; ++i) {cell temp_people = people[i];addedge(start, temp_people.num, 1, 0);debug(start, temp_people.num, 1, 0);}// house到汇点建边, 花费0, 流量1 for (int i = 0; i < house_num; ++i) {cell temp_house = house[i];addedge(temp_house.num, end, 1, 0);debug(temp_house.num, end, 1, 0);}// people到house建边,花费为曼哈顿距离,流量为1 for (int i = 0; i < people_num; ++i) {for (int j = 0; j < house_num; ++j) {cell temp_house = house[j];cell temp_people = people[i];int temp_dis = fabs(temp_house.x - temp_people.x) + fabs(temp_house.y - temp_people.y);addedge(temp_people.num, temp_house.num, 1, temp_dis);debug(temp_people.num, temp_house.num, 1, temp_dis);}}int ans = 0;        mincost(start, end, ans);printf("%d\n", ans);}return 0;
}

HDU Problem - 1533 Going Home(费用流板子题)相关推荐

  1. HUD -- 1533 Going Home(费用流基础题)

    题目大意: 有一个n*m的大小地图,图上有相等数量的小矮人和房子,每个小矮人只能水平或垂直走,小矮人每移动一步要花费一美元,每个房子只能容纳一个人,问要让所有小矮人都进房子里的最小花费是多少. 这是我 ...

  2. HDU Problem - 4289 Control(最大流)

    题目链接 Problem Description You, the head of Department of Security, recently received a top-secret inf ...

  3. HDU Problem - 4292 Food(最大流, 建边)

    题目链接 Problem Description You, a part-time dining service worker in your college's dining hall, are n ...

  4. P2153 晨跑,费用流裸题

    晨跑 题目连接 https://www.luogu.org/problemnew/show/P2153 题解 求最大不相交路径数,并在路径数最大前提下,求总路程最短. 太裸了. 求不相交路径数:将除1 ...

  5. P2604 ZJOI2010 网络扩容,费用流裸题

    网络扩容 题目链接 https://www.luogu.org/problemnew/show/P2604 题解 对于每条边u→vu \rightarrow vu→v,我们将按照容量=C=C=C,费用 ...

  6. P2053 SCOI2007 修车,费用流好题

    修车 题目链接 https://www.luogu.org/problemnew/show/P2053 题解 每个人每次只能修一辆车,且这个人修的最后一辆车所花时间为111倍的修这辆车的时间,修倒数第 ...

  7. 网络流 最大流 最小割 费用流

    [腾讯文档]网络流初步 网络流初步 文章目录 网络流初步 一.网络流简介 1. 网络 2. 流 3. 再次理解网络流 二.常见题型(三种) 三.相关问题对应算法介绍 1.最大流 (1) FF算法 - ...

  8. BZOJ 1920 Luogu P4217 [CTSC2010]产品销售 (模拟费用流、线段树)

    题目链接 (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=1920 (luogu) https://www.luogu.org/prob ...

  9. UOJ #455 [UER #8]雪灾与外卖 (贪心、模拟费用流)

    题目链接 http://uoj.ac/contest/47/problem/455 题解 模拟费用流,一个非常神奇的东西. 本题即为WC2019 laofu的讲课中的Problem 8,经典的老鼠进洞 ...

最新文章

  1. 【组队学习】【31期】基于Python的办公自动化
  2. java string转bytebuf,如何将Java字符串转换为字节[]?
  3. redhat5 oracle11g安装全程详解,RedHat5+Oracle11g安装全程详解.doc
  4. C# 字符串操作:split、substring、Format
  5. 视频操作_02视频追踪:meanshift算法+Camshift算法
  6. python获取文件名不含后缀名_大部分Python资料都没有说到的重点-用实战教你解决问题的思路...
  7. oracle 查出所有空表,Oracle查看某个用户上的所有空表
  8. 中国塑料食品和饮料包装行业市场供需与战略研究报告
  9. html5画布作品,10个会让你惊叹不已的HTML5画布(CANVAS)技术应用演
  10. not in与NOT EXISTS亲历的差别体验
  11. Atitit pagging翻页与按需加载图像 vue lazyload懒加载目录1.1. 翻最好就是不翻页直接加载一千数据咯 11.2. 使用VueLazyload 11.3. 五.更加方
  12. Centos7以上远程连接2003-Can't connect to MySQL server on 'localhost'(10060 Unkn...)
  13. echarts 3D 柱状图
  14. LEF和GDS匹配问题
  15. PS亮度蒙版工具:Lumenzia for Mac(支持ps2022)
  16. JAVA中RandomAccess接口
  17. php视频降清晰度,HTML5视频播放器-video-js(带清晰度切换) | 小灰灰博客
  18. android+微信支付
  19. 蚂蚁特工吱指南|用来吃的AR游戏机,奥利奥终于对自己下手了
  20. hda vs sda

热门文章

  1. S5P4418/S5P6818核心板EMMC已由原来的4.5版本升级到5.1版本
  2. angular2 组件交互
  3. python——面向对象篇之异常和反射
  4. 学霸网站-Beta版本发布说明
  5. Nagios+pnp4nagios+rrdtool 安装配置nagios(一)
  6. 64位虚拟机下asm()语法_一步步学写Windows下的Shellcode
  7. [Pyhon大数据分析] 二.PyEcharts绘制全国各地区、某省各城市地图及可视化分析
  8. LeetCode Algorithm 1052. 爱生气的书店老板
  9. Django 模型 —— 字段类型
  10. 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1109:开关灯