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.

InputThere 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. 
OutputFor 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

题意:使得每个m都对应一个H时的最短的路径

题解:找一个源点,连接所有的m,再找一个汇点,使得所有H指向这个汇点。

然后跑一边最大费用最小流就ok了

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAXX=;
const int INF=0x3f3f3f3f; struct node
{
int st;
int to;
int next;
int cap;
int cost;
}edge[MAXX]; char mp[][];
int head[MAXX],tol;
int pre[MAXX],dis[MAXX];
bool vis[MAXX];
int n,m,p; struct node1
{
int x,y;
node1(){}
node1(int a,int b)
{
x=a;
y=b;
}
};
vector<node1> v1,v2; void init()
{
tol=;
memset(head,-,sizeof(head));
v1.clear();
v2.clear();
} void addedge(int u,int v,int cap,int cost)
{
edge[tol].st=u;
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].cost=cost;
edge[tol].next=head[u];
head[u]=tol++; edge[tol].st=v;
edge[tol].to=u;
edge[tol].cap=;
edge[tol].cost=-cost;
edge[tol].next=head[v];
head[v]=tol++;
} bool SPFA(int s,int t)
{
queue<int> q;
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
memset(pre,-,sizeof(pre));
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front(); q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int to=edge[i].to;
if(edge[i].cap>&&dis[to]>dis[u]+edge[i].cost)
{
dis[to]=dis[u]+edge[i].cost;
pre[to]=i;
if(!vis[to])
{
vis[to]=;
q.push(to);
}
}
}
}
if(pre[t]==-)return ;
return ;
} int minCostMaxFlow(int s,int t)
{
int cost=;
while(SPFA(s,t))
{
int minn=INF;
for(int i=pre[t];i!=-;i=pre[edge[i].st])
minn=min(minn,edge[i].cap); for(int i=pre[t];i!=-;i=pre[edge[i].st])
{
edge[i].cap-=minn;
edge[i^].cap+=minn;
}
cost+=minn*dis[t];
}
return cost;
} int main()
{
while(scanf("%d%d",&n,&m)&&m&&n)
{
getchar();
init();
for(int i=;i<n;i++)
{
scanf("%s",mp[i]);
getchar();
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(mp[i][j]=='m')
v1.push_back(node1(i,j));
if(mp[i][j]=='H')
v2.push_back(node1(i,j)); }
int l=v1.size();
int r=v2.size();
for(int i=;i<v1.size();i++)
{ node1 N1=v1[i];
int x=i+;
addedge(,x,,);
for(int j=;j<v2.size();j++)
{
node1 N2=v2[j];
int y=j+l+;
int D=abs(N1.x-N2.x)+abs(N1.y-N2.y);
addedge(x,y,,D);
addedge(y,x,,D);
if(i==l-)
addedge(y,l+r+,,);
}
}
int ans=minCostMaxFlow(,l++r);
printf("%d\n",ans);
}
return ;
}

Going Home HDU - 1533(最大费用最小流)的更多相关文章

  1. POJ 2195 & HDU 1533 Going Home(最小费用最大流)

    这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...

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

    Farm Tour POJ - 2135 When FJ's friends visit him on the farm, he likes to show them around. His farm ...

  3. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  4. hdu 1533 Going Home 最小费用最大流 入门题

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. hdu 4322(最大费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4322 思路:建图真的是太巧妙了!直接copy大牛的了: 由于只要得到糖就肯定有1个快乐度,在这一点上糖 ...

  6. HDU 4322Candy 最大费用最大流

    由于被小孩子不喜欢的糖果的对小孩产生的效力是一样的,所以我们在网络流的时候先不考虑. 1 - 源点0到1~N个糖果,容量为1,费用为02 - 根据like数组,like[i][j] == 1时在糖果j ...

  7. hdu 1533 Going Home 最小费用最大流 (模板题)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. HDU 4406 最大费用最大流

    题意:现有m门课程需要复习,已知每门课程的基础分和学分,共有n天可以复习,每天分为k个时间段,每个时间段可以复习一门课程,并使这门课程的分数加一,问在不挂科的情况下最高的绩点. 思路:(没做过费用流的 ...

  9. 【进阶——最小费用最大流】hdu 1533 Going Home (费用流)Pacific Northwest 2004

    题意: 给一个n*m的矩阵,其中由k个人和k个房子,给每个人匹配一个不同的房子,要求所有人走过的曼哈顿距离之和最短. 输入: 多组输入数据. 每组输入数据第一行是两个整型n, m,表示矩阵的长和宽. ...

随机推荐

  1. 从IBM的计划中分析出中国重新相当然的错误选择吗

    <IBM欲用物联网技术解决北京雾霾难题> http://security.zol.com.cn/469/4690141.html 读了上文.分析出的. 因为我没有太多的数据.不好分析. 有 ...

  2. 汉澳Sinox2014X64server高级桌面服务器版操作系统公布

    汉澳Sinox2014X64server高级桌面服务器版操作系统公布   当你在现代城市夜空中看到一道闪电.屏幕中央闪过几个图形,转眼间变成美轮美奂的紫色空中天国,说明你来到了汉澳sinox2014世 ...

  3. java异步编程

    异步编程提供了一个非阻塞事件驱动的模型.通过异步消除阻塞,可以让web服务响应更多请求.可以让系统更高效的执行.比如log框架,记录日志或异常时异步执行可避免影响正常业务流程的执行. 异步变成如何把线 ...

  4. nginx搭建基于http协议的视频点播服务器

    1,于由自己的服务器上已经安装好nginx(具体安装方法见我的另一篇文章,Linux中安装nginx),所以不再安装. 2,下载nginx_mod_h264_streaming-2.2.7.tar.g ...

  5. 特征选择--->卡方选择器

    特征选择(Feature Selection)指的是在特征向量中选择出那些“优秀”的特征,组成新的.更“精简”的特征向量的过程.它在高维数据分析中十分常用,可以剔除掉“冗余”和“无关”的特征,提升学习 ...

  6. 36.面板Ext.Panel使用

    转自:https://www.cnblogs.com/linjiqin/archive/2011/06/22/2086620.html 面板Ext.Panel使用 概要 1.Ext.Panel概述 2 ...

  7. Vue.prototype的用法

    基础事例: 在vue项目main.js文件中: Vue.prototype.$appName = 'My App' 这样你可以通过在原型上定义它们使其在每个 Vue 的实例中可用. new Vue({ ...

  8. jqxtree异步加载部门树

    整体思路 A.要想实现异步加载第一次加载的是一级部门 B.加载一级部门,如果有子部门,部门前面带+号,没有子部门,部门前面没有+号(+号也就是点击可以展开) C.在sql中实现如果有子部门默认都加载一 ...

  9. 通过类库ChineseChar实现将汉字转化为拼音

    //封装dllusing Microsoft.International.Converters.PinYinConverter;using System.Text;namespace Utils{ p ...

  10. markdownpad2下载安装教程

    1.下载安装 http://markdownpad.com/download/markdownpad2-setup.exe 直接下载,安装过程中提醒要安装微软的一个什么环境,不用理会直接跳过,实测没有 ...