poj 2195 Going Home

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

题目大意:给你一个N × M的含有“H”, “m”。 “.”的二维图。“H”代表HOME。 “m”代表man,“.”代表空。man到HOME的距离为两点坐标的X坐标之差的绝对值加上Y坐标之差的绝对值。man的数量与HOME的数量同样。

如今。问,要是全部的man都回到HOME走的最短的路程总和为多少。

解题思路:还是建图的问题。依据这张二位的图,建一张流图。设置一个超级源点,连向全部的man,容量为1。设置一个超级汇点。使全部的HOME都连向超级汇点,容量为1。

把全部的HOME都拆成两个点。连接的容量为1。每一个房子仅仅能住一个人。

每一个man连向全部的房子,容量为1。费用为二维图上,man点到HOME点的距离。图建完以后。求最小费最大流。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std; const int N = 505;
const int NN = 105;
const int MM = 15005;
const int INF = 0x3f3f3f3f;
const int OF = 100;
const int FIN = 500;
typedef long long ll; int n, m, cntH, cntM, s, t;
struct Node{
int x, y;
}H[NN], M[NN];
char gra[NN][NN];
int pre[N], inq[N];
ll a[N], d[N];
struct Edge{
int from, to;
ll cap, flow;
ll cos;
}; vector<Edge> edges;
vector<int> G[MM]; void init() {
for (int i = 0; i < MM; i++) G[i].clear();
edges.clear();
} void addEdge(int from, int to, ll cap, ll flow, ll cos) {
edges.push_back((Edge){from, to, cap, 0, cos});
edges.push_back((Edge){to, from, 0, 0, -cos});
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
} int getDis(int x, int y) {
return abs(M[x].x - H[y].x) + abs(M[x].y - H[y].y);
} void input() {
cntH = cntM = 0;
for (int i = 0; i < n; i++) scanf("%s", gra[i]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (gra[i][j] == 'H') {
H[cntH].x = i;
H[cntH++].y = j;
} else if (gra[i][j] == 'm') {
M[cntM].x = i;
M[cntM++].y = j;
}
}
}
for (int i = 1; i <= cntH; i++) {
addEdge(i, i + OF, 1, 0, 0);
}
for (int i = 1; i <= cntM; i++) {
for (int j = 1; j <= cntH; j++) {
addEdge(i + 2 * OF, j, 1, 0, getDis(i - 1, j - 1));
}
}
for (int i = 1; i <= cntM; i++) {
addEdge(0, i + 2 * OF, 1, 0, 0);
}
for (int i = 1; i <= cntH; i++) {
addEdge(i + OF, t, 1, 0, 0);
}
} int BF(int s, int t, ll& flow, ll& cost) {
queue<int> Q;
memset(inq, 0, sizeof(inq));
memset(a, 0, sizeof(a));
memset(pre, 0, sizeof(pre));
for (int i = 0; i < N; i++) d[i] = INF;
d[s] = 0;
a[s] = INF;
inq[s] = 1;
int flag = 1;
pre[s] = 0;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = 0;
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cos) {
d[e.to] = d[u] + e.cos;
a[e.to] = min(a[u], e.cap - e.flow);
pre[e.to] = G[u][i];
if (!inq[e.to]) {
inq[e.to] = 1;
Q.push(e.to);
}
}
}
flag = 0;
}
if (d[t] == INF) return 0;
flow += a[t];
cost += (ll)d[t] * (ll)a[t];
for (int u = t; u != s; u = edges[pre[u]].from) {
edges[pre[u]].flow += a[t];
edges[pre[u]^1].flow -= a[t];
}
return 1;
} int MCMF(int s, int t, ll& cost) {
ll flow = 0;
cost = 0;
while (BF(s, t, flow, cost));
return flow;
} int main() {
while (scanf("%d %d\n", &n, &m) == 2, n, m) {
s = 0, t = FIN;
init();
input();
ll cost;
MCMF(s, t, cost);
printf("%lld\n", cost);
}
return 0;
}

poj 2195 Going Home(最小费最大流)的更多相关文章

  1. POJ 2195 Going Home 最小费用最大流 尼玛,心累

    D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  2. POJ 2195 - Going Home - [最小费用最大流][MCMF模板]

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...

  3. poj 2195 Going Home(最小费用最大流)

    题目:http://poj.org/problem?id=2195 有若干个人和若干个房子在一个给定网格中,每人走一个都要一定花费,每个房子只能容纳一人,现要求让所有人进入房子,且总花费最小. 构造一 ...

  4. poj 2135 Farm Tour 最小费最大流

    inf开太小错了好久--下次还是要用0x7fffffff #include<stdio.h> #include<string.h> #include<vector> ...

  5. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

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

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

  7. 【POJ 2195】 Going Home(KM算法求最小权匹配)

    [POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  8. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

  9. POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏

    Going Home Description On a grid map there are n little men and n houses. In each unit time, every l ...

随机推荐

  1. HDOJ 4876 ZCC loves cards

    枚举组合,在不考虑连续的情况下推断能否够覆盖L...R,对随机数据是一个非常大的减枝. 通过检測的暴力计算一遍 ZCC loves cards Time Limit: 4000/2000 MS (Ja ...

  2. MFC中添加ToolTip提示框

    PART 1 MFC 对话框中的 Buttton添加提示 例如我们想在一个对话框中的一个button控件添加tooltip,实现的方法如下: 1. 在该对话框的类中添加一个CToolTipCtrl类型 ...

  3. SpringBoot 使用小技巧合集

    原文:https://my.oschina.net/xiedeshou/blog/1926191 设置网站图标 原来我们在使用tomcat开发时,设置网站图片时,即icon图标时,一般都是直接替换ro ...

  4. Log4j输出格式控制

    参数说明例子 %c 列出logger名字空间的全称,如果加上{<层数>}表示列出从最内层算起的指定层数的名字空间 log4j配置文件参数举例 输出显示媒介 假设当前logger名字空间是& ...

  5. 26个Jquery1.4使用小技巧

    1. 禁止右键点击 1.       $(document).ready(function(){ 2.           $(document).bind("contextmenu&quo ...

  6. Wordpress中文章的特色图像Featured Image究竟存在哪里?

    最近项目需要,分析了一下Wordpress的特色图像 Feature Image的上传.保存方式,这一分析觉得Wordpress的数据结构设计还真是有想法. 先简单说一下结论: Wordpress中图 ...

  7. CentOS 加载/挂载 U盘 (转)

    原文链接:CentOS 加载/挂载 U盘 Linux如何加载(优)U盘 1,以root用户登陆    先加载USB模块 modprobe usb-storage    用fdisk -l 看看U盘的设 ...

  8. C语言:返回两个数组中第一个元素的指针,并输出这个值

    // //  main.c //  Pointer_search // //  Created by ma c on 15/8/2. //  Copyright (c) 2015年. All righ ...

  9. 第二十三章 springboot + 全局异常处理

    一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...

  10. Android数据填充器LayoutInflater

    LayoutInflater类在应用程序中比较实用,可以叫布局填充器,也可以成为打气筒,意思就是将布局文件填充到自己想要的位置,LayoutInflater是用来找res/layout/下的xml布局 ...