题面

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

题解

题目大意:给定一张地图,m是人,H是房子,现在每个人都要到一栋房子去,问所有人到房子的曼哈顿距离之和最小是多少?

题解:

可以用KM做

考虑用费用流,

每个人向房子连一条容量为1,费用为曼哈顿距离的边

求最小费用流即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAX 200
#define MAXL 200000
#define INF 1000000000
struct Line
{
int v,next,w,fb,fy;
}e[MAXL];
int ff,hh,mm,ww;
struct Node
{
int x,y;
};
vector<Node> H,M;
int h[MAX*MAX],cnt=1,cost;
int pe[MAX*MAX],pr[MAX*MAX];
inline void Add(int u,int v,int w,int fy)
{
e[cnt]=(Line){v,h[u],w,cnt+1,fy};
h[u]=cnt++;
e[cnt]=(Line){u,h[v],0,cnt-1,-fy};
h[v]=cnt++;
}
int dis[MAX*MAX],S,T,n,m;
char g[MAX][MAX];
bool vis[MAX*MAX];
bool SPFA()
{
memset(dis,63,sizeof(dis));
dis[S]=0;
queue<int> Q;while(!Q.empty())Q.pop();
Q.push(S);
memset(vis,0,sizeof(vis));
while(!Q.empty())
{
int u=Q.front();Q.pop();
vis[u]=false;
for(int i=h[u];i;i=e[i].next)
{
int f=dis[u]+e[i].fy,v=e[i].v;
if(e[i].w&&dis[v]>f)
{
dis[v]=f;
pe[v]=i;
pr[v]=u;
if(!vis[v])
{
vis[v]=true;
Q.push(v);
}
}
}
}
if(dis[T]==dis[T+1])return false;//增广失败
int re=INF;
for(int v=T;v!=S;v=pr[v])
re=min(re,e[pe[v]].w);//计算增广的最大流
for(int v=T;v!=S;v=pr[v])
{
e[pe[v]].w-=re;
e[e[pe[v]].fb].w+=re;
}
ff+=re;
cost+=re*dis[T];
return true;
}
int main()
{
//freopen("POJ2195.in","r",stdin);
while(233)
{
scanf("%d%d",&n,&m);
cnt=1;
memset(h,0,sizeof(h));
if(n==0&&m==0)break;
H.clear();M.clear();hh=mm=cost=ff=0;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
cin>>g[i][j];
if(g[i][j]=='H')
{
H.push_back((Node){i,j});
hh++;
}
if(g[i][j]=='m')
{
M.push_back((Node){i,j});
mm++;
}
}
S=0;T=mm+hh+1;
for(int i=0;i<M.size();++i)
{
Add(S,i+1,1,0);
for(int j=0;j<H.size();++j)
{
Add(i+1,1+j+mm,1,abs(M[i].x-H[j].x)+abs(M[i].y-H[j].y));//曼哈顿距离
}
}
for(int i=1;i<=hh;++i)
Add(i+mm,T,1,0);
while(SPFA());
printf("%d\n",cost);
}
return 0;
}

POJ 2195 Going Home (费用流)的更多相关文章

  1. poj - 2195 Going Home (费用流 || 最佳匹配)

    http://poj.org/problem?id=2195 对km算法不理解,模板用的也不好. 下面是大神的解释. KM算法的要点是在相等子图中寻找完备匹配,其正确性的基石是:任何一个匹配的权值之和 ...

  2. POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

    http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  3. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  4. Going Home POJ - 2195 (最小费用最大流)

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  5. POJ 3680 Intervals(费用流)

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5762   Accepted: 2288 Descrip ...

  6. POJ 2175 Evacuation Plan 费用流 负圈定理

    题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在 ...

  7. POJ 3680 Intervals(费用流+负权优化)

    [题目链接] http://poj.org/problem?id=3680 [题目大意] 有N个带权重的区间,现在要从中选取一些区间, 要求任意点都不被超过K个区间所覆盖,请最大化总的区间权重. [题 ...

  8. poj 2135 Farm Tour 费用流

    题目链接 给一个图, N个点, m条边, 每条边有权值, 从1走到n, 然后从n走到1, 一条路不能走两次,求最短路径. 如果(u, v)之间有边, 那么加边(u, v, 1, val), (v, u ...

  9. BZOJ3502PA2012Tanie linie&BZOJ2288[POJ Challenge]生日礼物——模拟费用流+链表+堆

    题目描述 n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. 输入 输出 样例输入 5 2 7 -3 4 -9 5 样例输出 13   根据 ...

随机推荐

  1. smallcorgi/Faster-RCNN_TF训练自己的数据

    熟悉了github项目提供的训练测试后,可以来训练自己的数据了.本文只介绍改动最少的方法,只训练2个类, 即自己添加的类(如person)和 background,使用的数据格式为pascal_voc ...

  2. spring boot学习资源

    http://blog.csdn.net/u014695188/article/details/52226134 http://www.jianshu.com/p/887c22723e43 Sprin ...

  3. Linux下的压力测试工具:ab、http_load、webbench、siege

    一.ab 1.1 介绍 ab是apache自带的一款功能强大的测试工具.      安装了apache一般就自带了. 1.2 下载 同apache. 1.3 安装 同apache. 1.4 安装结果 ...

  4. [记]WIndow/Linux 获取本机(全部)IPv4、IPv6、MAC地址方法 (C/C++)

    Linux 获取本机IP.MAC地址用法大全 //#include <sys/types.h> #include <ifaddrs.h> #include <sys/io ...

  5. [bzoj4551][Tjoi2016&Heoi2016]树-树链剖分

    Brief Description 给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个 结点,可以打多次标记.) ...

  6. thinkPHP替换SQL变量

    使用tp里M()->where(pb_id=%d and course=%d and DATE_FORMAT(pub_time, \"%H:%i:%s\") < &qu ...

  7. 利用Azure嵌套虚拟化,解决公有云上机器不能启动的问题

    很多时候我们都会碰到因为意外重启,机器硬盘被损坏导致无法启动,或者是因为各种原因Windows上的RDP服务启动不了,Linux上的SSH无法链接等等问题.碰到这种问题基本上很难解决以前都是将VHD下 ...

  8. centos/linux下的安装Nginx

    1.安装gcc编译器 先查看gcc编译器是否安装 在shell控制台输入gcc-v 如果没有安装请看下一步 使用yuma安装gcc yum intsall gcc 看到如下视图则说明安装成功 2.安装 ...

  9. (转载)SVM-基础(三)

    支持向量机: Kernel  by pluskid, on 2010-09-11, in Machine Learning     70 comments 本文是"支持向量机系列" ...

  10. 在linux系统中

    A .etc下放置配置文件 B./var下放置日志文件 C./root超级用户主目录 D./home 使用者家目录 /bin  二进制执行文件,也就是命令文件 /etc 下存放的是配置文件 /dev ...