题目链接: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. SpringMVC由浅入深day01_8springmvc和mybatis整合

    8 springmvc和mybatis整合 为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合. 整合目标:控制层采用springmvc ...

  2. 8 -- 深入使用Spring -- 7...4 使用自动装配

    8.7.4 使用自动装配 在自动装配策略下,Action还是由Spring插件创建,Spring 插件在创建Action实例时,利用Spring的自动装配策略,将对应的业务逻辑组件注入Action实例 ...

  3. 接口测试之JMeter初探

    1.JMeter安装配置 )登录 http://jmeter.apache.org/download_jmeter.cgi ,下载与自己的平台相对应文件: )安装JDK(.6以上),配置环境变量JAV ...

  4. mkubimage-mlc2: error while loading shared libraries: liblzo2.so.2: cannot open shared object file: No such file or directory

    mkubimage-mlc2: error while loading shared libraries: liblzo2.so.2: cannot open shared object file: ...

  5. 敏感词过滤和XML的创建

    今天我慢下来啦,因为这三天没有新的课程学习内容,自己仅仅看啦一些,这让我停下来栖息片刻:说说现在的生活,简单的进行着,每天要奔波着去上课,然后回来,每天都在想怎样学习这个小知识点,大脑也在想怎样解决程 ...

  6. 在 Core Data 中存取 transformable 类型的数据

    本文转载至 http://imenjoe.com/2015/04/10/CoreData-transformable-20150410/ 在开发过程中有一个需要在 Core Data 中存取 NSDi ...

  7. SpringMVC系列之URL匹配问题

    一.工程目录 二.web.xml配置文件及与其他文件的关系 三.控制器部分 四.返回值 五.url前后缀 六.项目源代码 http://files.cnblogs.com/files/xujian20 ...

  8. Kindeditor问题

    1.初始的时候,拿到切图,kindeditor不知道为什么就是显示不出来,只出来个文本框一样的东西 原因:样式未加载,这个是因为美工那边样式重调了,而且新项目并没有将整个插件拷贝过来,而只是拿了kin ...

  9. vs必备快捷键整理

    .格式化代码:Ctrl+E,D .格式化部分代码:选中代码->Ctrl+K,F.或者Ctrl+E,F. .折叠cs文件所有方法:Ctrl+M,O .打开或折叠所有代码:Ctrl+M,L (打开或 ...

  10. C语言变量的存储布局

    分析以下代码中变量存储空间如何分配: //MemSeg.c: 代码无意义,仅供分析用 #include <stdio.h> #include <stdlib.h> //mall ...