题目链接:http://poj.org/problem?id=2195

Time Limit: 1000MS Memory Limit: 65536K

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
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6732762

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

构图:
把man作为一个顶点集合U,house作为另一个顶点集合V,把U中所有点到V中所有点addedge{from∈U,to∈V,cap=1,cost=两点间哈密顿距离},构成一个多源多汇的二分图。
构造一个超级源点s和超级汇点t:s与U中所有点相连,费用为0,容量为1;V中所有点与t相连,费用为0,容量为1。

这种构图思路很简单,我们建立起的二分图:

不难想象,maxflow必然等于the number of men(or the number of houses),在上图的例子中,即为3;

由于我们在所有man点与house点间建立的边容量为1,显然,最大流保证了:

  ①所有man都能到house里;

  ②一个man只进到一间house,一间house只有一个man;

就像在上图中,最大流的情况下,每个m[i]只能选一个H[j],而且i,j也不能重复;

这恰好就满足了题目所给的要求,然后,只要我们在m[i]到H[j]的边上加上cost=Hamiltonian_distance(m[i],H[j]),求出最小费用就是答案啦。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
#define MAXN 203
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
int u,v,c,f,a;
};
struct MCMF
{
int s,t;
vector<Edge> E;
vector<int> G[MAXN];
int vis[MAXN];
int d[MAXN];
int pre[MAXN];
int aug[MAXN];
void init(int l,int r)
{
for(int i=l;i<=r;i++) G[i].clear();
E.clear();
}
void addedge(int from,int to,int cap,int cost)
{
E.push_back((Edge){from,to,cap,,cost});
E.push_back((Edge){to,from,,,-cost});
int m=E.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool SPFA(int s,int t,int &flow,int &cost)
{
memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));
d[s]=, vis[s]=, pre[s]=, aug[s]=INF;
queue<int> q;
q.push(s);
while(!q.empty())
{
int now=q.front(); q.pop();
vis[now]=;
for(int i=;i<G[now].size();i++)
{
Edge& e=E[G[now][i]];
int nex=e.v;
if(e.c>e.f && d[nex]>d[now]+e.a)
{
d[nex]=d[now]+e.a;
pre[nex]=G[now][i];
aug[nex]=min(aug[now],e.c-e.f);
if(!vis[nex])
{
q.push(nex);
vis[nex]=;
}
}
}
}
if(d[t]==INF) return ;
flow+=aug[t];
cost+=d[t]*aug[t];
for(int i=t;i!=s;i=E[pre[i]].u)
{
E[pre[i]].f+=aug[t];
E[pre[i]^].f-=aug[t];
}
return ;
} int mincost()
{
int flow=,cost=;
while(SPFA(s,t,flow,cost));
return cost;
}
}mcmf; char map[][];
struct Node{int row,col;};
int Hami_dist(Node a,Node b){return abs(a.row-b.row) + abs(a.col-b.col);} int main()
{
int N,M;//N行,M列
while(scanf("%d%d",&N,&M) && N!=)
{
vector<Node> men,houses;
for(int i=;i<=N;i++) scanf("%s",map[i]+);
for(int i=;i<=N;i++)
{
for(int j=;j<=M;j++)
{
if(map[i][j]=='m') men.push_back((Node){i,j});
if(map[i][j]=='H') houses.push_back((Node){i,j});
}
}
int n=men.size();
mcmf.init(,*n+);
mcmf.s=, mcmf.t=*n+;
for(int i=;i<n;i++)
{
mcmf.addedge(mcmf.s,i+,,);
for(int j=;j<n;j++)
{
if(i==) mcmf.addedge(j++n,mcmf.t,,);
mcmf.addedge(i+,j++n,,Hami_dist(men[i],houses[j]));
}
}
printf("%d\n",mcmf.mincost());
}
}

POJ 2195 - Going Home - [最小费用最大流][MCMF模板]的更多相关文章

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

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

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

    题目:http://poj.org/problem?id=2195 有若干个人和若干个房子在一个给定网格中,每人走一个都要一定花费,每个房子只能容纳一人,现要求让所有人进入房子,且总花费最小. 构造一 ...

  3. 网络流--最小费用最大流MCMF模板

    标准大白书式模板 #include<stdio.h> //大概这么多头文件昂 #include<string.h> #include<vector> #includ ...

  4. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  5. poj2135最小费用最大流经典模板题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13509   Accepted: 5125 Descri ...

  6. 把人都送到房子里的最小花费--最小费用最大流MCMF

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=1533 相邻的容量为inf,费用为1,S到m容量为1,费用为0 ,H到T容量为1,费用为0. 建图跑-最小费 ...

  7. POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]

    ---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...

  8. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  9. POJ 3680: Intervals【最小费用最大流】

    题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...

随机推荐

  1. mysql asyn 示例

    这篇文章摘自mysql asyn作者的博客,博客地址 开头有一个简单示例,然后是一个在play上的应用.例子我并没有跑过,但是仍能学到不少东西. object BasicExample { def m ...

  2. 有人在群里问mysql如何选择性更新部分条件的问题

    有人在群里问这个问题 update xt_kh set zhye=zhye+1,hzyj=hzyj+1 where dlgh='kiss0451' and hzms=1 如果这样写 hzms不等于1的 ...

  3. HttpClinet学习笔记

    本文为学习httpClient学习过程中转载的文章,若涉及版权请留言. ----------------------------- 前言 超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议 ...

  4. PostgreSQL存储过程(5)-异常错误处理

    1. 异常错误处理 在PL/pgSQL函数中,如果没有异常捕获,函数会在发生错误时直接退出,与其相关的事物也会随之回滚.我们可以通过使用带有EXCEPTION子句的BEGIN块来捕获异常并使其从中恢复 ...

  5. RF根据单个/多个output文件重新生成log和report文件

    场景1:根据单个output文件重新生成log和report文件命令: rebot -d 日志和报告文件保存路径  output.xml文件 场景2:合并两个不同路径下的output文件并生成新的lo ...

  6. bat批处理文件按顺序执行exe

    start /d "deepstream.io-windows-3.1.0" deepstream.exe rem 等待1秒钟 choice /t 1 /d y /n cd &qu ...

  7. Linux下的/proc目录介绍

    proc被称为虚拟文件系统,它是一个控制中心,可以通过更改其中某些文件改变内核运行状态, 它也是内核提空给我们的查询中心,用户可以通过它查看系统硬件及当前运行的进程信息. Linux中许多工具的数据来 ...

  8. springbatch---->springbatch的使用(七)

    这里我们讲述一下springbatch中关于step层面上面的数据共享技术.而对街的人影都浸染在一片薄荷的白色中,由于无声,都好像经过漂染,不沾人间烟火. step的数据共享 关于springbatc ...

  9. [转]C++中模板的特化与偏特化

    转载自:http://hi.baidu.com/klcdyx2008/blog/item/5adbf77b79f316f90bd1873c.html 1.引言C++中的模板分为类模板和函数模板,虽然它 ...

  10. QT开发之旅四邮件发送工具

    终于有了一个晚上安静的写写程序,最近一直忙着公司商务上的事情,一直想用QT实现一个调用最底层socket通信来实现的邮件发送程序,以前用C#写过,微软都封装好的,不知道底层是如何实现的,只知道调用方法 ...