Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17955   Accepted: 9145

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
扫描路径得到人和房子的坐标,相减得所费路程,然后建边最小费用流即可
 #include<cstdio>
#include <queue>
#include <algorithm>
#include <assert.h>
#include <cstring>
using namespace std;
const int inf=0x7fffffff;
int n,m; char maz[][];
int man[][];
int house[][];
int hlen,mlen; const int sups=;
const int supt=; int cost[][];
int f[][];
int e[][];
int len[]; int d[];
int pre[];
bool vis[]; queue<int >que; int main(){
while(scanf("%d%d",&n,&m)==&&n&&m){
input:
hlen=mlen=;
gets(maz[]);
for(int i=;i<n;i++){
gets(maz[i]);
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(maz[i][j]=='m'){
man[mlen][]=i;man[mlen++][]=j;
}
else if(maz[i][j]=='H'){
house[hlen][]=i;house[hlen++][]=j;
}
}
} memset(f,,sizeof(f));
memset(cost,,sizeof(cost));
fill(len,len+mlen,hlen+);fill(len+mlen,len+mlen+hlen,mlen+);len[sups]=mlen;len[supt]=hlen;
bulidedge:
for(int i=;i<mlen;i++){
f[sups][i]=;
e[sups][i]=i;
e[i][]=sups;
for(int j=;j<hlen;j++){
e[i][j+]=j+mlen;
e[j+mlen][i+]=i;
f[i][j+mlen]=;
cost[i][j+mlen]=abs(man[i][]-house[j][])+abs(man[i][]-house[j][]);
cost[j+mlen][i]=-cost[i][j+mlen];
}
}
for(int j=;j<hlen;j++){
e[supt][j]=j+mlen;
e[j+mlen][]=supt;
f[j+mlen][supt]=;
} mincostflow:
int ans=;
int flow=mlen;
while(flow>){
fill(d,d+,inf);
d[sups]=;
que.push(sups);
while(!que.empty()){
int fr=que.front();que.pop();
vis[fr]=false;
for(int i=;i<len[fr];i++){
int to=e[fr][i];
if(f[fr][to]>&&d[to]>d[fr]+cost[fr][to]){
d[to]=d[fr]+cost[fr][to];
pre[to]=fr;
if(!vis[to]){
que.push(to);
vis[to]=true;
}
}
}
} assert(d[supt]!=inf); int sub=flow;
for(int i=supt;i!=sups;i=pre[i]){
sub=min(flow,f[pre[i]][i]);
}
flow-=sub;
ans+=sub*d[supt];
for(int i=supt;i!=sups;i=pre[i]){
f[pre[i]][i]-=sub;
f[i][pre[i]]+=sub;
} }
printf("%d\n",ans);
}
return ;
}

POJ 2195 Going Home 最小费用流 难度:1的更多相关文章

  1. POJ 2195 Going Home 最小费用流 裸题

    给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...

  2. POJ 2195 Going Home 最小费用流

    POJ2195 裸的最小费用流,当然也可以用KM算法解决,但是比较难写. 注意反向边的距离为正向边的相反数(因此要用SPFA) #include<iostream> #include< ...

  3. POJ 2516 Minimum Cost 最小费用流 难度:1

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 13511   Accepted: 4628 Des ...

  4. POJ 2195 Going Home 最小费用最大流 尼玛,心累

    D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  5. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

  6. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  7. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

  8. poj 2195 Going Home(最小费最大流)

    poj 2195 Going Home Description On a grid map there are n little men and n houses. In each unit time ...

  9. 【POJ 2195】 Going Home(KM算法求最小权匹配)

    [POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

随机推荐

  1. Python3基础 str format 输出花括号{}

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. ubuntu下交叉编译lftp

    一.背景: lftp依赖于ncurses,readline和gnutls 二.准备工作 2.1交叉编译ncurses 2.1.1获取ncurses源码 wget ftp://ftp.invisible ...

  3. LightOJ 1296 Again Stone Game(sg函数)题解

    题意:每次必须拿且只能拿不超过一半的石头,不能拿为败 思路:显然算出每个的sg函数,但是范围1e9显然不能直接打表.所以先打表找规律,发现偶数一直是自己的一半,奇数好像没规律.偶数x的sg函数值是x/ ...

  4. HDU 4734 (数位DP)题解

    思路: dp[pos][pre]代表长度为pos的不大于pre的个数 #include<iostream> #include<cstdio> #include<cstri ...

  5. Spring资源加载基础ClassLoader

    1 ClassLoader工作机制 1.1 ClassLoader作用 寻找类字节码文件并构造出类在JVM内部表示的组件.负责运行时查找和装入Class字节码文件 1.2 装载步骤 1.2.1 装载 ...

  6. HDU 1711 Number Sequence(KMP模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...

  7. Centos下挖XMR门罗币的详细教程

    很多朋友都看过我之前写的Ubuntu下挖XMR门罗币的教程,也有很多朋友提出,为什么不写个Centos的教程出来,今天我在这里就写个Centos的教程,看这个教程前,大家先看看之前的教程,因为里面涉及 ...

  8. 如何新建一个datatable,并往表里赋值

    顺序是新建对象-->新建列-->新建行,示例代码如下: DataTable dt=new DataTable(); //新建对象 dt.Columns.Add("姓名" ...

  9. python删除所有自定义变量方法--转载

    http://blog.sina.com.cn/s/blog_b2f983a50102yexs.html   当我们在pythonwin中创建多个变量后,通过dir()函数,可以看到所有已创建变量,这 ...

  10. React Native基础概念和基础认识

    学习地址:https://github.com/vczero/react-native-lesson 当我们初始化一个RN项目的时候主要的是index.ios.js文件和index.android.j ...