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. 获取GetOpenFileName多选文件名

    void CWriteWnd::OpenFileDialog() { OPENFILENAME ofn; TCHAR szOpenFileNames[*MAX_PATH] = _T("&qu ...

  2. linux下保存下位机输出的串口信息为文件

    linux下保存下位机输出的串口信息为文件 1.stty -F /dev/ttyUSB0 raw (转换成raw模式) 2.stty -F /dev/ttyUSB0 speed 115200 (设置波 ...

  3. HDU1556 Color the ball(差分数组)题解

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. mysql-5.7.20-winx64.zip Zip版、解压版MySQL安装

    1.  zip下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-winx64.zip 2.官方文档位置: http:// ...

  5. Unity3D学习笔记(一):Unity简介、游戏物体、组件和生命周期函数

    Project(工程.项目):工程是把游戏开发当前所需要的资源归类管理用的. Console控制台:日志.报错.调试,右上角,消息过滤 Assets:资源,存储游戏中一切用到的资源 Library:临 ...

  6. NetMagic Simple Overview

    参考: NetMagic Startup: How to develop NetMagic rapidly NetMagic Simple Overview NetMagic 是什么? NetMagi ...

  7. FOJ-1001-Duplicate Pair

    题目:Duplicate Pair 大意: 有多组测试数据,输入整数n,接着输入n个整数(integers),这些数字几乎都只出现一次,但其中有一个数出现了两次,请输出这个数. 题解: 大数据问题,普 ...

  8. UVa 1626 括号序列(矩阵连乘)

    https://vjudge.net/problem/UVA-1626 题意: 输入一个由 "(" . ")" . "[" . " ...

  9. hdu 1573 X问题 两两可能不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  10. c语言 快速排序

    #include<stdio.h> #include<stdlib.h> #define BUF_SIZE 10 void display(int array[], int m ...