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. Others - On Duty

    On Duty This is xxx and will be duty engineer in the next week. Thanks. Here is a kindly reminder. T ...

  2. vue中封装公共方法,全局使用

    1.以封装的合并单元格为例,首先建立一个util.js 2.在main.js中引用 3.直接在使用该方法的地方调用就可以了

  3. 调用的执行器“executor://mstestadapter/v2”时发生异常: 无法找到程序集“log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a”

    加上下面的一句就好了 [TestCleanup]      public void cleanup()      {          CallContext.FreeNamedDataSlot(&q ...

  4. WebForm - cookie赋值乱码问题

    cookie的值为中文时候,取cookie的值会出现乱码 解决办法:存取cookie时候先解码和编码 存cookie,进行编码: cookie.Value = HttpUtility.UrlEncod ...

  5. VS2015 WPF Prism Xaml Designer error

    Ref: http://wiki.tk2kpdn.com/build-error-prism5-interactionrequesttrigger-with-vs2015/ gacutil -i &q ...

  6. ThreadLocal的练习代码

    场景: 有三个小孩儿,买了一个变形金刚玩具(Transformer).... 三个小孩都争着玩这个玩具....没有一个人可以玩... 第一种方式:每个人各玩一会.... 第二种方式:再买两个玩具,一个 ...

  7. syslog之二:syslog协议及rsyslog服务全解析

    目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...

  8. git自己用得着的命令

    -----------随笔记记,给自己备份------------ 1.查看分支 查看当前分支:git branch 查看远程所有分支:git branch -r/git branch -a 2.切换 ...

  9. sparkshell运行sql报错: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    下载msyql的连接driver https://download.csdn.net/download/xz360717118/10662304 把其中一个: mysql-connector-java ...

  10. 【从0到1学javascript】javascript数据结构----数组

    javascript中对数组的定义 数组是一种特殊的对象,用来表示偏移量的索引是该对象的属性,索引可以是整数.这些数字索引在内部被转换成字符串类型.这是因为javascript对象中的属性名必须是字符 ...