Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15944   Accepted: 8167

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

 
题意:

给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致。man每移动一格需花费$1(即单位费用=单位距离),一间house只能入住一个man。现在要求所有的man都入住house,求最小费用。

man可以从house上跨过

 
思路:
 
模板题   建图之后 直接套最小费用流模板
 
#include <stdio.h>
#include <iostream>
#include <string.h>
#include<cmath>
using namespace std;
const int N=300;
const int MAXE=200000;
const int inf=1<<30;
int head[N],ep;
int d[N],pre[N];
bool vis[N];
int q[MAXE];
struct Edge
{
int u,v,c,w,next;
}edge[MAXE];
void addedge(int u,int v,int w,int c)//u v 费用 容量
{
edge[ep].u=u;
edge[ep].v=v;
edge[ep].w=w;
edge[ep].c=c;
edge[ep].next=head[u];
head[u]=ep++;
edge[ep].v=u;
edge[ep].u=v;
edge[ep].w=-w;
edge[ep].c=0;
edge[ep].next=head[v];
head[v]=ep++;
}
int SPFA(int src,int des)
{
int l,r;
memset(pre,-1,sizeof(pre));
memset(vis,0,sizeof(vis));
for(int i=0;i<=des;i++) d[i]=inf;
d[src]=0;
l=0;r=0;
q[r++]=src;
vis[src]=1;
while(l<r)
{
int u=q[l++];
vis[u]=0;
for(int j=head[u];j!=-1;j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].c>0&&d[u]+edge[j].w<d[v])
{
d[v]=d[u]+edge[j].w;
pre[v]=j;
if(!vis[v])
{
vis[v]=1;
q[r++]=v;
}
}
}
}
if(d[des]==inf)
return 0;
return 1;
}
int MCMF(int src,int des)
{
int flow=0,ans=0;//flow是得到的最大流的值 ans得到的是最小的费用
while(SPFA(src,des))
{
ans+=d[des];
int u=des;
int mini=inf;
while(u!=src)
{
if(edge[pre[u]].c<mini)
mini=edge[pre[u]].c;
u=edge[pre[u]].u;
}
flow+=mini;
u=des;
while(u!=src)
{
edge[pre[u]].c-=mini;
edge[pre[u]^1].c+=mini;
u=edge[pre[u]].u;
}
}
return ans;
}
///以上为模板
struct man
{
int x;
int y;
}M[111];
struct house
{
int x,y;
}H[111];
char str[111];
int main()
{
int n,m,mcnt,hcnt,i,j,src,des;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n&&!m) break;
ep=0;
memset(head,-1,sizeof(head));
mcnt=hcnt=0;
for(i=0;i<n;i++)
{
scanf("%s",str);
for(j=0;j<m;j++)
{
if(str[j]=='H')
{
hcnt++;
H[hcnt].x=i; H[hcnt].y=j;
}
else if(str[j]=='m')
{
mcnt++;
M[mcnt].x=i; M[mcnt].y=j;
}
}
}
for(i=1;i<=mcnt;i++)
{
for(j=1;j<=hcnt;j++)
{
int dis=abs(M[i].x-H[j].x)+abs(M[i].y-H[j].y);
addedge(i,mcnt+j,dis,1);
//addedge(mcnt+j,i,dis,1);
}
}
src=0;
des=mcnt+hcnt+1;
for(i=1;i<=mcnt;i++)
addedge(src,i,0,1);
for(j=1;j<=hcnt;j++)
addedge(mcnt+j,des,0,1);
int ans=MCMF(src,des); printf("%d\n",ans);
}
return 0;
}

最小费用最大流模板 poj 2159 模板水题的更多相关文章

  1. 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  2. 最小费用最大流(luogu P3381 【模板】最小费用最大流)

    题目链接 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S. ...

  3. hdu 6437 /// 最小费用最大流 负花费 SPFA模板

    题目大意: 给定n,m,K,W 表示n个小时 m场电影(分为类型A.B) K个人 若某个人连续看了两场相同类型的电影则失去W 电影时间不能重叠 接下来给定m场电影的 s t w op 表示电影的 开始 ...

  4. SGU 185.Two shortest (最小费用最大流)

    时间限制:0.25s 空间限制:4M 题意: 在n(n<=400)个点的图中,找到并输出两条不想交的最短路.不存在输出“No sulotion”: Solution: 最小费用最大流 建图与po ...

  5. poj 2195 最小费用最大流模板

    /*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...

  6. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  7. 最小费用最大流模板(POJ 2135-Farm Tour)

    最近正好需要用到最小费用最大流,所以网上就找了这方面的代码,动手写了写,先在博客里存一下~ 代码的题目是POJ2135-Farm Tour 需要了解算法思想的,可以参考下面一篇文章,个人觉得有最大流基 ...

  8. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

  9. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

随机推荐

  1. Linux下PHP安装配置MongoDB数据库连接扩展

    Web服务器: IP地址:192.168.21.127 PHP安装路径:/usr/local/php 实现目的: 安装PHP的MongoDB数据库扩展,通过PHP程序连接MongoDB数据库 具体操作 ...

  2. (转)iOS Wow体验 - 第六章 - 交互模型与创新的产品概念(1)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第六章译文精选,其余章节将陆续放出.上一篇:Wow ...

  3. Spark Accumulators

    概述 Accumulator即累加器,与Mapreduce counter的应用场景差不多,都能很好地观察task在运行期间的数据变化,Spark中的Accumulator各task可以对Accumu ...

  4. JS高级程序设计学习笔记之JS事件(1)

    事件流 冒泡 定义:事件开始时由最具体的元素接收,然后逐级上传到较为不具体的节点.(IE9.FF.Chrome.Safari会将事件一直冒泡到window对象.IE5.5及其以下会跳过<html ...

  5. css Reset文件

    /* KISSY CSS Reset 理念:清除和重置是紧密不可分的 特色:1.适应中文 2.基于最新主流浏览器 维护:玉伯(lifesinger@gmail.com), 正淳(ragecarrier ...

  6. 简单JS多级下拉框无刷新

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Oracle语句块PL/SQL循环判断

    - --pl/sql Procedural Language /sql --被数据库编译保存,由用户调用 --程序块 /* 语法 Declare – 声明变量 --声明变量 Age int; //没有 ...

  8. hdu4296 贪心

    E - 贪心 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit I ...

  9. hdu Counting Sheepsuanga

    算法:深搜 题意:让你判断一共有几个羊圈: 思路:像四个方向搜索: Problem Description A while ago I had trouble sleeping. I used to ...

  10. [转载]opencv +linux

    转载 ubuntu12.04安装openCV2.4.2(2012-08-08 16:54:06 参考http://www.samontab.com/web/2012/06/installing-ope ...