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. Jmeter_前端RSA加密下的登陆模拟_引用js文件实现

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在一次项目实战中,前端登录使用了RSA加密,使用LoadRunner压测的第一步,就是模拟用户登录,可惜loadRunner11并不能录制前端的加 ...

  2. 31_网络编程-struct

    一.struct   1.简述  我们可以借助一个模块,这个模块可以把要发送的数据长度转换成固定长度的字节.这样客户端每次接收消息之前只要先接受这个固定长度字节的内容看一看接下来要接收的信息大小,那么 ...

  3. underscore.js源码研究(7)

    概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...

  4. 设置 Linux 下打印机的几种方式

    设置 Linux 下打印机的几种方式 一.使用 cups 进行设置 如若遇到 cups 也没有驱动的话可以前往 openprinting.org 找寻对应驱动. 二.前往 official 下载驱动 ...

  5. django~项目的文件位置的重要性

    前几天我犯了个很低级的错误 就是把文件的地址放错地方了~~ 我把templates文件放进mysite文件里面了 和templatetags文件同级了  所以一直报错  说找不到模板的文件 实际上te ...

  6. oracle数据库迁移相关

    常见的实现方式: rman exp/imp  expdp/impdp DG OGG 主要是看停机时间了,方法很多,数据量小,就导出,如果时间要求很高,那可以采取dg或ogg或类似的技术.减低downt ...

  7. vs2017 对dockerfile的支持

    项目添加 dockerfile Docker file 内容 FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base WORKDIR /app EXP ...

  8. 【原创】Your Connection is not private

    用Chrome打开google等https网站时碰到问题: “your connection is not private”. 后来发现是跟GoAgent的安全证书有关系(我用XX.NETFQ) 解决 ...

  9. (转)centos 7 Tomcat 8.5 的安装及生产环境的搭建调优

    原文:https://www.cnblogs.com/linhankbl/articles/9149804.html#top JVM菜鸟进阶高手之路七(tomcat调优以及tomcat7.8性能对比) ...

  10. rtp header

    rtp协议基于udp传输,流媒体音视频数据被封装在rtp中,通过rtp协议进行实时的传输. 一.rtp协议头格式 The RTP header has a minimum size of 12 byt ...