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. mac tips

    1. Mac Terminal color for different types 在 ~ 先建立一个文件  ~/.bash_profile 加入下面的两行:export CLICOLOR=1expo ...

  2. jquery生产和开发的区别

    今天说一下jquery生产和开发的区别,在我们下载jquery的时候,会有两个下载链接,一个是jquery.min.js .迷你版 (生产),另一个是 jquery.js .开发版 .不知道的人可能就 ...

  3. Activity.startManagingCursor方法

    http://blog.sina.com.cn/s/blog_6f14deb60100wd2n.html 总结一下Activity.startManagingCursor方法: 转 我们将获得的Cur ...

  4. HashMap HashTable HashSet

    原文转载自 http://blog.csdn.net/wl_ldy/article/details/5941770 HashMap是新框架中用来代替HashTable的类 也就是说建议使用HashMa ...

  5. iOS开发UI篇——Button基础

    一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状态 1. normal(普通状态) ...

  6. php文件粘贴上传

    <?php header("Access-Control-Allow-Origin:*"); $url = 'http://'.$_SERVER['HTTP_HOST']; ...

  7. 安卓学习之ListView和GridView

    ListView 和 GridView是安卓中显示信息的两个很基本也最常用的控件.他们的用法很相似,但是他俩也是有区别的. ListView显示的数据会将他的item放在一行显示,而且根据内容给出it ...

  8. SGU 111.Very simple problem

    题目大意:              求平方不大于n(n<=10^1000)的最大的数. 分析:              二分+高精度乘法 或者 高精度开方...               ...

  9. 【转载】【挖掘Treap的潜力】

    原帖: http://fanhq666.blog.163.com/blog/static/819434262011021105212299/ 你的Treap能支持以下操作吗?1.区间增减 2.区间求最 ...

  10. amf0解释一下

    就简单记录一下省了以后忘了,amf0其实就几种数据格式的网络传输格式,比如数字,字符串,这些格式在传输的时候他给单独序列化了一下,主要支持以下这些: #define AMF0_NUMBER ((uin ...