Going Home

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23515   Accepted: 11853

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

Source

 
所有的房子和超级源点连边,容量为1,费用为0。
所有的人和超级汇点连边,容量为1,费用为0。
所有的房子和所有的人相互连边,容量为1,费用为房子和人的曼哈顿距离。
 
 //2017-08-24
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int to, next, c, w;//c为容量,w为单位费用
}edge[M]; void add_edge(int u, int v, int c, int w){
edge[tot].c = c;
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].c = ;
edge[tot].w = -w;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} bool vis[N];
int pre[N], dis[N];//pre记录路径,dis记录到源点的最小花费
struct MinCostMaxFlow{
int S, T;
int flow, cost;
void init(int _S, int _T){
S = _S;
T = _T;
tot = ;
memset(head, -, sizeof(head));
}
bool spfa(){
memset(vis, , sizeof(vis));
memset(dis, INF, sizeof(dis));
dis[S] = ;
vis[S] = ;
queue<int> que;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(edge[i].c > && dis[v] > dis[u]+edge[i].w){
dis[v] = dis[u] + edge[i].w;
pre[v] = i;
if(!vis[v]){
vis[v] = true;
que.push(v);
}
}
}
vis[u] = ;
}
return dis[T] != INF;
}
int dfs(int &flow){
int u, p, sum = INF, ans = ;
for(u = T; u != S; u = edge[p^].to){
//记录路径上的最小流值
p = pre[u];
sum = min(sum, edge[p].c);
}
for(u = T; u != S; u = edge[p^].to){
p = pre[u];
edge[p].c -= sum;
edge[p^].c += sum;
ans += sum*edge[p].w;
}
flow += sum;
return ans;
}
int maxflow(){
cost = , flow = ;
while(spfa()){//寻找增广路并增广
cost += dfs(flow);
}
return cost;
}
}mcmf; string grid[N];
int x[N], y[N]; int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputD.txt", "r", stdin);
int n, m;
while(cin>>n>>m && (n || m)){
int cnt_m = , cnt_h = ;
int s = N-, t = N-;
mcmf.init(s, t);
for(int i = ; i < n; i++){
cin>>grid[i];
for(int j = ; j < m; j++){
if(grid[i][j] == 'H'){
add_edge(s, cnt_h, , );
x[cnt_h] = i;
y[cnt_h++] = j;
}
}
}
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
if(grid[i][j] == 'm'){
add_edge(cnt_h+cnt_m, t, , );
for(int k = ; k < cnt_h; k++){
add_edge(k, cnt_h+cnt_m, , abs(i-x[k])+abs(j-y[k]));
}
cnt_m++;
}
}
}
cout<<mcmf.maxflow()<<endl;
} return ;
}

POJ2195&&HDU1533(KB11-D 最小费用最大流)的更多相关文章

  1. hdu1533 Going Home 最小费用最大流 构造源点和汇点

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. POJ2195 Going Home —— 最大权匹配 or 最小费用最大流

    题目链接:https://vjudge.net/problem/POJ-2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. POJ2195:Going Home (最小费用最大流)

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26212   Accepted: 13136 题目链接 ...

  4. [hdu1533]二分图最大权匹配 || 最小费用最大流

    题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...

  5. 最小费用最大流 POJ2195-Going Home

    网络流相关知识参考: http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 出处:優YoU http://blog.csdn. ...

  6. POJ 2195 Going Home 【最小费用最大流】

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:2715 ...

  7. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  8. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  9. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  10. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

随机推荐

  1. sed 横排扩展

    sed "$!N;s/\n/KEY/" FILENAME

  2. pm2 官方文档 学习笔记

    一.安装 1.安装 npm install pm2 -g 2.更新 npm install pm2 -g && pm2 update pm2 update 是为了刷新 PM2 的守护进 ...

  3. JS获取浏览器URL中查询字符串的参数

    首先要知道Location这个对象以及这个对象中的一些属性: href:设置或返回完整的url.如本博客首页返回http://www.cnblogs.com/wymninja/ host:设置或返回主 ...

  4. JAVA虚拟机体系结构JAVA虚拟机的生命周期

    一个运行时的Java虚拟机实例的天职是:负责运行一个java程序.当启动一个Java程序时,一个虚拟机实例也就诞生了.当该程序关闭退出,这个虚拟机实例也就随之消亡.如果同一台计算机上同时运行三个Jav ...

  5. Java Web(四) 一次性验证码的代码实现

    其实实现代码的逻辑非常简单,真的超级超级简单. 1.在登录页面上login.jsp将验证码图片使用标签<img src="xxx">将绘制验证码图片的url给它 2.在 ...

  6. POJ 2491

    #include<iostream>#include<stdio.h>#include<string>#define MAXN 400using namespace ...

  7. div在页面垂直居中方法---增强改进版

    div在页面垂直居中方法---改进版 .wrap{ background: #ffffff; position:absolute; margin:auto; top:; bottom:; left:; ...

  8. windows 系统安装git的方法

    windows 系统安装git的方法 msysgit是Windows版的Git,从https://git-for-windows.github.io下载 安装默认步骤,一步步安装即可 安装完成后,在开 ...

  9. Android从零开始

    Android开发环境的安装 1 IDE Android可以使用开发的IDE有Eclipse 或者 Android Studio.Android Studio还处于v 0.1.x版本,是early a ...

  10. dubbo-001--前言

    1,前言: 第一次了解dubbo是在15年刚工作的时候在瑞友,项目是给凯撒旅游公司签证部做一个签证系统,项目需要调用一些凯撒的方法,比如权限.部门信息,跟签证相关的一些如签证国家城市.领区啥玩意的,等 ...