Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16200   Accepted: 8283

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的矩阵,其中每个'm'代表一个人,每个‘H'代表一个房子,且人和房子的数目相同,要求每个人找到一个属于自己的房子,每个房子只能住一个人,人到房子的花费就是它们之间的曼哈顿距离。问他们的最小花费。 思路: 最小费用最大流问题,先建图:
    建立一个超级源点s和一个超级汇点t.
    s 指向所有的人,容量为1,费用为0;
    每个人指向所有的房子,容量为1,费用为人和房子的曼哈顿距离;
    所有房子指向t,容量为1,费用为0。
从源点到汇点求费用流。
 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std; const int INF = 0x3f3f3f3f;
const int maxn = ;
struct node
{
int x,y;
}man[maxn],house[maxn];
int man_num,house_num;
int flow[maxn][maxn];
int cost[maxn][maxn];
int dis[maxn],parent[maxn];
int s,t;
int mincost; void spfa()
{
queue<int> que;
int inque[maxn];
while(!que.empty())
que.pop();
for(int i = s; i <= t; i++)
dis[i] = INF;
memset(inque,,sizeof(inque));
memset(parent,-,sizeof(parent)); que.push(s);
inque[s] = ;
dis[s] = ;
while(!que.empty())
{
int u = que.front();
que.pop();
inque[u] = ; for(int v = s; v <= t; v++)
{
if(flow[u][v] && dis[v] > dis[u] + cost[u][v])
{
dis[v] = dis[u] + cost[u][v];
parent[v] = u;
if(!inque[v])
{
inque[v] = ;
que.push(v);
}
}
}
}
} void MCMF()
{
mincost = ;
while()
{
spfa();//spfa在残量网络中找s-t最短路;
if(parent[t] == -) break;//若汇点不可达,表明当前流已是最小费用最大流。 for(int u = t; u != s; u = parent[u])//增广
{
flow[parent[u]][u] -= ;
flow[u][parent[u]] += ;
}
mincost += dis[t];//更新总费用;
}
} int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
if(n == && m == ) break;
getchar();
char str[];
man_num = ;
house_num = ; for(int i = ; i <= n; i++)
{
scanf("%s",str);
for(int j = ; str[j]; j++)
{
if(str[j] == 'm')
{
man[man_num].x = i;
man[man_num++].y = j+;
}
else if(str[j] == 'H')
{
house[house_num].x = i;
house[house_num++].y = j+;
}
}
}
memset(flow,,sizeof(flow));
memset(cost,,sizeof(cost));
s = ;//源点
t = man_num+house_num+;//汇点
for(int i = ; i < man_num; i++)
flow[s][i+] = ;//源点到所有人的容量为1
for(int i = ; i < man_num; i++)
{
for(int j = ; j < house_num; j++)
{
//每个人到所有房子的容量为1,费用为其曼哈顿距离;
flow[i+][j++man_num] = ;
cost[i+][j++man_num] = abs(man[i].x-house[j].x) + abs(man[i].y-house[j].y);
cost[j++man_num][i+] = -cost[i+][j++man_num];
}
}
for(int i = ; i < house_num; i++)
flow[i++man_num][t] = ;//所有房子到汇点的容量为1
MCMF();
printf("%d\n",mincost);
}
return ;
}

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

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

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

  2. bzoj1927最小费用最大流

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

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

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

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

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

  5. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

  6. 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 821  Solved: 502[Submit][Status ...

  7. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

  8. UVa11082 Matrix Decompressing(最小费用最大流)

    题目大概有一个n*m的矩阵,已知各行所有数的和的前缀和和各列所有数的和的前缀和,且矩阵各个数都在1到20的范围内,求该矩阵的一个可能的情况. POJ2396的弱化版本吧..建图的关键在于: 把行.列看 ...

  9. UVa12092 Paint the Roads(最小费用最大流)

    题目大概说一个n个点m条带权有向边的图,要给边染色,染色的边形成若干个回路且每个点都恰好属于其中k个回路.问最少要染多少边权和的路. 一个回路里面各个点的入度=出度=1,那么可以猜想知道各个点如果都恰 ...

  10. POJ3686 The Windy's(最小费用最大流)

    题目大概说要用m个工厂生产n个玩具,第i个玩具在第j个工厂生产要Zij的时间,一个工厂同一时间只能生成一个玩具,问最少的用时. 这题建的图不是很直观.. 源点向玩具连容量1费用0的边 将每个工厂拆成n ...

随机推荐

  1. media query

    accepted Another useful media feature is device-aspect-ratio. Note that the iPhone 5 does not have a ...

  2. Linux软件安装,RPM与YUM

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3843955.html ...

  3. 原生JS与jQuery文档加载完毕的写法

    HTML是有执行顺序的,默认是自上而下执行.所以当我们的js代码在html代码下边的时候,可以正常执行,而当我们的js代码在html代码上边的时候,可以就无法正常执行了,这时,我们需要在文档加载完毕的 ...

  4. weex APIs

    1.通过这个$vm()上下文访问这些api在脚本的方法 <script> module.exports = { methods: { somemethod: function() { th ...

  5. Erlang - Download and Install for Linux

    1. 下载 Erlang [huey@huey-K42JE erlang]$ wget http://www.erlang.org/download/otp_src_R16B03.tar.gz 2. ...

  6. IIS7.5 asp+access数据库连接失败处理 64位系统

    IIS7.5 asp+access数据库连接失败处理(SRV 2008R2 x64/win7 x64) IIS7.5不支持oledb4.0驱动?把IIS运行模式设置成32位就可以了,微软没有支持出64 ...

  7. HTML5 文件域+FileReader 读取文件并上传到服务器(三)

    一.读取文件为blob并上传到服务器 HTML <div class="container"> <!--读取要上传的文件--> <input type ...

  8. one way WebService

    WSDL支持4种消息交换方式:   1)单向(One-way):服务端接收消息:   2)请求响应(Request-response):服务端点接收请求消息,然后发送响应消息:   3)要求应答(So ...

  9. [转帖]MATLAB曲线绘制及颜色类型

    信号源产生的方法 来源:http://www.2cto.com/kf/201401/270494.html  matlab的checkerboard说明,GOOD! 来源:http://www.chi ...

  10. PHP代码批量加密

    <?php error_reporting(E_ALL); ini_set('display_errors','1'); //批量加密码当前目录 $dirnow = getcwd(); $dir ...