Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22088   Accepted: 11155

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

Source


每个人和每个房子连边,二分图最大权匹配
用spfa费用流求解(或者KM)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=,M=1e6+,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,s,t,n1,n2;
char ss[];
struct data{
int x,y;
}a[N],b[N];
inline int dis(data &a,data &b){return abs(a.x-b.x)+abs(a.y-b.y);} struct edge{
int v,ne,c,f,w;
}e[M<<];
int cnt,h[N];
inline void ins(int u,int v,int c,int w){
cnt++;
e[cnt].v=v;e[cnt].c=c;e[cnt].f=;e[cnt].w=w;
e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].c=;e[cnt].f=;e[cnt].w=-w;
e[cnt].ne=h[v];h[v]=cnt;
}
void build(){
cnt=;
memset(h,,sizeof(h));
s=;t=n1+n2+;
for(int i=;i<=n1;i++)
for(int j=;j<=n2;j++) ins(i,n1+j,,dis(a[i],b[j]));
for(int i=;i<=n1;i++) ins(s,i,,);
for(int i=;i<=n2;i++) ins(n1+i,t,,);
}
int d[N],q[N],head,tail,inq[N],pre[N],pos[N];
inline void lop(int &x){if(x==N)x=;}
bool spfa(){
memset(d,,sizeof(d));
memset(inq,,sizeof(inq));
head=tail=;
d[s]=;inq[s]=;q[tail++]=s;
pre[t]=-;
while(head!=tail){
int u=q[head++];inq[u]=;lop(head);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(d[v]>d[u]+w&&e[i].c>e[i].f){
d[v]=d[u]+w;
pre[v]=u;pos[v]=i;
if(!inq[v])q[tail++]=v,inq[v]=,lop(tail);
}
}
}
return pre[t]!=-;
}
int mcmf(){
int flow=,cost=;
while(spfa()){
int f=INF;
for(int i=t;i!=s;i=pre[i]) f=min(f,e[pos[i]].c-e[pos[i]].f);
flow+=f;cost+=d[t]*f;
for(int i=t;i!=s;i=pre[i]){
e[pos[i]].f+=f;
e[((pos[i]-)^)+].f-=f;
}
}
return cost;
}
int main(int argc, const char * argv[]){
while(true){
n1=n2=;
n=read();m=read();
if(n==&&m==) break;
for(int i=;i<=n;i++){
scanf("%s",ss+);
for(int j=;j<=m;j++){
if(ss[j]=='H') a[++n1]=(data){i,j};
if(ss[j]=='m') b[++n2]=(data){i,j};
}
}
build();
printf("%d\n",mcmf());
}
}
 
 
 
 

POJ2195 Going Home[费用流|二分图最大权匹配]的更多相关文章

  1. 【BZOJ 3308】 3308: 九月的咖啡店 (费用流|二分图最大权匹配)

    3308: 九月的咖啡店 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 244  Solved: 86 Description 深绘里在九份开了一家咖 ...

  2. POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏

    Going Home Description On a grid map there are n little men and n houses. In each unit time, every l ...

  3. [hdu1533]二分图最大权匹配 || 最小费用最大流

    题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...

  4. 【bzoj3291】Alice与能源计划 模拟费用流+二分图最大匹配

    题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验. 为了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...

  5. @noi.ac - 507@ 二分图最大权匹配

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 有一天你学了一个能解决二分图最大权匹配的算法,你决定将这个算法应 ...

  6. Hdu2255 奔小康赚大钱(二分图最大权匹配KM算法)

    奔小康赚大钱 Problem Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好 ...

  7. [ACM] HDU 2255 奔小康赚大钱 (二分图最大权匹配,KM算法)

    奔小康赚大钱 Problem Description 传说在遥远的地方有一个很富裕的村落,有一天,村长决定进行制度改革:又一次分配房子. 这但是一件大事,关系到人民的住房问题啊. 村里共同拥有n间房间 ...

  8. HDU2255 奔小康赚大钱 —— 二分图最大权匹配 KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    ...

  9. 二分图最大权匹配——KM算法

    前言 这东西虽然我早就学过了,但是最近才发现我以前学的是假的,心中感慨万千(雾),故作此篇. 简介 带权二分图:每条边都有权值的二分图 最大权匹配:使所选边权和最大的匹配 KM算法,全称Kuhn-Mu ...

随机推荐

  1. 使用NPOI组件完成的Excel导出导入(附源代码,测试通过)

    最近遇到一个Excel导入导出的问题,要支持winform和webform,这里我是一个认真严谨的coder,所以决定把这个记录下来!和大家一起分享一下!如果需要的同学可以下载哦! 对于NPOI这个组 ...

  2. Thinking in Java——笔记(20)

    Annotations They provide information that you need to fully describe your program, but that cannot b ...

  3. 第 31 章 项目实战-PC端固定布局[1]

    学习要点: 1.准备工作 2.创建项目 3.网站结构 4.CSS选择器 5.完成导航 主讲教师:李炎恢 本章主要开始使用学习用HTML5和CSS3来构建Web页面,第一个项目采用PC端 固定布局来实现 ...

  4. linux 共享内存 shmat,shmget,shmdt,shmctl

    shmget int shmget(key_t key, size_t size, int flag);//开辟一段共享内存 key_t key :标识符的规则() size_t size :共享内存 ...

  5. 【夯实PHP系列】购物车代码说明PHP的匿名函数

    1. 定义:匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应 ...

  6. javascript如何设置DIV背景色为随机色

    随机色有两种格式: 效果预览:http://wjf444128852.github.io/DEMOLIST/JS/test/index.html 1.rgb(xxx,xxx,xxx) 2.#xxxxx ...

  7. CSS中越界问题经典解决方案

    8.CSS相关知识 (1)如何解决父元素的第一个子元素的margin-top越界问题 1)为父元素加border-top: 1px;——有副作用 2)为父元素指定padding-top: 1px;—— ...

  8. Apache日志按天切割

    Linux系统配置方法: 将其改为 ErrorLog "| /usr/local/apache/bin/rotatelogs /home/logs/www/error_%Y%m%d.log ...

  9. GCD封装的个人理解和应用

    GCD封装的个人理解和应用 特点 >>将GCD封装,使我们从繁琐的方法记忆中解脱出来,能够直接快速的应用. 使用方法 1.将工程中的GCD文件中的9个文件拖入自己的工程中(你自己最好建一个 ...

  10. win7显示不是正版系统的解决方法

    十一长假回来,打开电脑就变成这样了.现在的我已经学会了不再逃避问题,要学着解决问题,就在网上搜集了有关这方面的信息.说是下载一个激活工具就可以了.我就试着下载了,但是不知道为什么下了几个激活工具都不管 ...