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 题意没什么好说的 看样例就知道题意了 直接套板子
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000") using namespace std;
const int maxn = 1e5 + ;
typedef long long LL;
const int MX = ;
const int inf = 0x3f3f3f3f;
const int MXE = MX * MX * ;
struct MinCost_MaxFlow {
struct Edge {
int v, w, nxt;
int cost;
} E[MXE];
int head[MX], tot, level[MX], pre[MX], d[MX];
bool vis[MX];
void init() {
memset(head, -, sizeof(head));
tot = ;
}
void add(int u, int v, int w, int cost) {
E[tot].v = v;
E[tot].w = w;
E[tot].cost = cost;
E[tot].nxt = head[u];
head[u] = tot++;
E[tot].v = u;
E[tot].w = ;
E[tot].cost = -cost;
E[tot].nxt = head[v];
head[v] = tot++;
}
bool spfa(int s, int t) {
memset(vis, , sizeof(vis));
memset(d, 0x3f, sizeof(d));
memset(pre, -, sizeof(pre));
queue<int>q;
q.push(s);
d[s] = ;
vis[s] = ;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = ;
for (int i = head[u]; ~i; i = E[i].nxt) {
int w = E[i].w, v = E[i].v, cost = E[i].cost;
if (w > && d[v] > d[u] + cost) {
d[v] = d[u] + cost;
pre[v] = i;
if (!vis[v]) {
q.push(v);
vis[v] = ;
}
}
}
}
//如果是最小费用可行流则要这一句(要求费用最小,不要求流量最大)
//if (d[t] > 0) return false;
return pre[t] != -;
}
int solve(int s, int t, int &cost) {
int flow = ;
cost = ;
while (spfa(s, t)) {
int minFlow = inf;
for (int i = pre[t]; ~i; i = pre[E[i ^ ].v])
minFlow = min(minFlow, E[i].w);
for (int i = pre[t]; ~i; i = pre[E[i ^ ].v]) {
cost += minFlow * E[i].cost;
E[i].w -= minFlow;
E[i ^ ].w += minFlow;
}
flow += minFlow;
}
return flow;
}
} F;
int n, m;
struct Point {
int x, y;
Point (int x, int y): x(x), y(y) {}
};
int cal(Point a, Point b) {
return abs(a.x - b.x) + abs(a.y - b.y);
}
char tu[][];
vector<Point>men;
vector<Point>home;
int main() {
while(~sff(n, m), n + m) {
F.init();
men.clear();
home.clear();
for (int i = ; i < n ; i++) {
scanf("%s", tu[i]);
for (int j = ; j < m ; j++) {
if (tu[i][j] == 'm') men.push_back(Point(i, j));
if (tu[i][j] == 'H') home.push_back(Point(i, j));
}
}
int s=,len1=men.size(),len2=home.size(),t;
t=len1+len2+;
for (int i= ;i<len1 ;i++)
for (int j= ;j<len2 ;j++)
F.add(i+,j++len1,,cal(men[i],home[j]));
for (int i= ;i<=len1 ;i++) F.add(,i,,);
for (int i= ;i<=len2 ;i++) F.add(i+len1,t,,);
int cost = ;
F.solve(s, t, cost);
printf("%d\n", cost);
}
return ;
}

Going Home POJ - 2195 费用流板子题的更多相关文章

  1. Going Home POJ - 2195(费用流)

    就是一个简单题 四个月前a的一道题,今天又看到了,再a一遍吧. 好吧 我想多了 用了bfs求最短路  其实不用的 因为没有障碍物 #include <iostream> #include ...

  2. poj 2516 (费用流)

    题意:有N个供应商,M个店主,K种物品.每个供应商对每种物品的的供应量已知,每个店主对每种物品的需求量的已知,从不同的供应商运送不同的货物到不同的店主手上需要不同的花费,又已知从供应商m送第k种货物的 ...

  3. poj 2175 费用流消圈

    题意抽象出来就是给了一个费用流的残存网络,判断该方案是不是最优方案,如果不是,还要求给出一个更优方案. 在给定残存网络上检查是否存在负环即可判断是否最优. 沿负环增广一轮即可得到更优方案. 考虑到制作 ...

  4. HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

    题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...

  5. Lunch Time(费用流变型题,以时间为费用)

    Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others)     ...

  6. Coding Contest(费用流变形题,double)

    Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...

  7. POJ 1087 最大流裸题 + map

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15597   Accepted: 5308 ...

  8. POJ 3686 The Windy's(思维+费用流好题)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5362   Accepted: 2249 Descr ...

  9. 网络流板子/费用流板子 2018南京I题+2016青岛G题

    2018南京I题: dinic,链式前向星,数组队列,当前弧优化,不memset全部数组,抛弃满流点,bfs只找一条增广路,每次多路增广 #include <bits/stdc++.h> ...

随机推荐

  1. 【WXS数据类型】Boolean

    属性: 名称 值类型 说明 [Boolean].constructor [String] 返回值为“Boolean”,表示类型的结构字符串 方法: 原型:[Boolean].toString() 说明 ...

  2. 【jmeter进阶之逻辑控制器】

    jmeter进阶之逻辑控制器  转载   https://www.cnblogs.com/malinalian/p/10491946.html   常用的逻辑控制器 1,循环控制器:可以设置该控制器内 ...

  3. Font Awesome 完美的图标字体

    好久没来,虽说鄙人的人气不咋地,但还是很想念自己这一亩二分田地. 近期用在平台开发中,看着设计师摆开阵势,准备大画图标,想着自己将会很KUBI拼凑css-sprite图片,接着写一大堆 class^= ...

  4. 初涉 JavaScript

    网页是什么 网页 = Html+CSS+JavaScriptHtml:网页元素内容CSS:控制网页样式JavaScript:操作网页内容,实现功能或者效果 JavaScirpt 发展历史 参考 使用 ...

  5. [HNOI2018]转盘

    [HNOI2018]转盘 给你一个 \(n\) 元环, 你可以在 \(0\) 时刻从任意一个位置出发, 每一秒可以选择往后或者留在原地每个点有个参数 \(T_i\) , 当你走到 \(i\) 的时间 ...

  6. scipy 图像处理-深度学习

    scipy 图像处理(scipy.misc.scipy.ndimage).matplotlib 图像处理 from scipy.misc import imread / imsave / imshow ...

  7. Python中的Dictionary

    Dictionary的创建 1 字面量 >>>D = {'a': 1, 'b': 2} >>>D {'b': 2, 'a': 1} 2 keyword参数 > ...

  8. windows编程入门最重要的

    要入门 Windows 编程,最重要的不是阅读什么教材,使用什么工具,而是先必须把以下几个对于初学者来说非常容易困惑的重要概念搞清楚: 1. 文字的编码和字符集.这部分需要掌握 ANSI 模式和 Un ...

  9. 转 Js 跨域CORS报错 Response for preflight has invalid HTTP status code 405

    转自:http://www.cnblogs.com/SilenceTom/p/6697484.html 调用接口遇到Response for preflight has invalid HTTP st ...

  10. ASP.NET 最全的POST提交数据和接收数据 —— (1) 用url传参方式

    //1.对象提交,字典方式 //接口方:public ActionResult GetArry(Car model) public void PostResponse() { HttpWebReque ...