2018.06.27Going Home(二分图匹配)
Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24716 Accepted: 12383
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
Pacific Northwest 2004
题意简述:平面内有相同数量的人和房子,一个人走一格需要111的花费,求人和房子全部匹配所需要的最小花费
不难发现如果把人和房子分成两个集合,将人和房子的曼哈顿距离看作是两两之间连上的边的权值的话,这题实际上是要求这个二分图的最小匹配,那么我们建一个源点和汇点,然后按如下的方式连边再跑最小费用流即可。
从sss到人:容量为111,权值为000。
从人到房子:容量为111,权值为两者的曼哈顿距离。
从房子到ttt:容量为ttt,权值为000
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
#define N 25000
#define M 200000
using namespace std;
inline long long read(){
long long ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
return ans;
}
struct Node{int v,next,c,w;}e[M];
struct node{int x,y;}l[105],r[105];
int d[N],flow[N],first[N],pred[N],pos[N],n,m,s,t,cnt,tot1,tot2;
bool in[N];
inline void add(int u,int v,int c,int w){
e[++cnt].v=v;
e[cnt].c=c;
e[cnt].w=w;
e[cnt].next=first[u];
first[u]=cnt;
e[++cnt].v=u;
e[cnt].c=0;
e[cnt].w=-w;
e[cnt].next=first[v];
first[v]=cnt;
}
inline bool spfa(){
queue<int>q;
memset(d,inf,sizeof(d));
memset(flow,inf,sizeof(flow));
memset(in,false,sizeof(in));
q.push(s),in[s]=true,d[s]=0,pred[t]=-1;
while(!q.empty()){
int x=q.front();
q.pop(),in[x]=false;
for(int i=first[x];i!=-1;i=e[i].next){
int v=e[i].v;
if(e[i].c>0&&d[v]>d[x]+e[i].w){
d[v]=d[x]+e[i].w,pred[v]=x,pos[v]=i,flow[v]=min(flow[x],e[i].c);
if(!in[v])in[v]=true,q.push(v);
}
}
}
return pred[t]!=-1;
}
inline void solve(){
int maxf=0,maxw=0;
while(spfa()){
maxf+=flow[t],maxw+=flow[t]*d[t];
int now=t;
while(now!=s){
e[pos[now]].c-=flow[t];
e[pos[now]^1].c+=flow[t];
now=pred[now];
}
}
printf("%d\n",maxw);
}
char S[150];
int main(){
while(1){
n=read(),m=read();
if(!n&&!m)break;
memset(first,-1,sizeof(first));
memset(e,0,sizeof(e));
cnt=-1,tot1=0,tot2=0;
for(int i=1;i<=n;++i){
scanf("%s",S+1);
for(int j=1;j<=m;++j){
if(S[j]=='H')l[++tot1].x=i,l[tot1].y=j;
if(S[j]=='m')r[++tot2].x=i,r[tot2].y=j;
}
}
s=0,t=tot1+tot2+1;
for(int i=1;i<=tot1;++i)add(s,i,1,0);
for(int i=tot1+1;i<=tot1+tot2;++i)add(i,t,1,0);
for(int i=1;i<=tot1;++i)
for(int j=1;j<=tot2;++j)
add(i,j+tot1,1,abs(l[i].x-r[j].x)+abs(l[i].y-r[j].y));
solve();
}
return 0;
}
2018.06.27Going Home(二分图匹配)的更多相关文章
- 2018.07.06 POJ2536 Gopher II(二分图匹配)
Gopher II Time Limit: 2000MS Memory Limit: 65536K Description The gopher family, having averted the ...
- UVa 二分图匹配 Examples
这些都是刘汝佳的算法训练指南上的例题,基本包括了常见的几种二分图匹配的算法. 二分图是这样一个图,顶点分成两个不相交的集合X , Y中,其中同一个集合中没有边,所有的边关联在两个集合中. 给定一个二分 ...
- UVA 12549 - 二分图匹配
题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...
- POJ 1274 裸二分图匹配
题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...
- BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2375 Solved: 1005[Submit][Sta ...
- HDU1281-棋盘游戏-二分图匹配
先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...
- HDU 1083 网络流之二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- BZOJ 1059 & 二分图匹配
题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...
随机推荐
- PHP依赖注入(DI)和控制反转(IoC)详解
这篇文章主要介绍了PHP依赖注入(DI)和控制反转(IoC)的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 首先依赖注入和控制反转说的是同一个东西,是一种设计模式,这种设计模式用来减少程 ...
- Eclipse创建Maven-Web项目及解决 jre版本和web.xml版本问题
eclipse maven-web有个蛋疼的问题,就是web版本很低. 且看别人是如何解决的:Eclipse创建Maven-Web项目及解决 jre版本和web.xml版本问题
- http get 方式参数的长度限制<转>
http get 方式参数的长度限制 这个问题一直以来似乎是被N多人误解,其实Http Get方法提交的数据大小长度并没有限制,而是IE浏览器本身对地址栏URL长度有最大长度限制:2048个字符. 当 ...
- ABAP-ALV报表导出格式恢复初始画面
进入一个ALV表格,想下载数据,一般点清单-->输出-->电子数据表. 会出来一个对话框,可选择导出成各类格式. 在下端有一个“始终使用选定的格式”,一旦勾上,就再也不会弹出选择框了. 以 ...
- sts,eclipse里面配置tomcat
第一步:点击window-->preferences-->server-->Runtime Environments点击add. 第二步:选择本机上面有的tomcat版本点击next ...
- JAVA WEB开发中的资源国际化
为什么要国际化? 不同国家与地区语言,文化,生活习惯等差异.在数字,时间,语言,货币,日期,百分数等的不同. 两个名词: I18N:即资源国际化,全称为Internationalization,因为首 ...
- Hibernate 的事物简单的增删查改
Hibernate 是一个优秀的ORM框架体现在: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管 ...
- scala -- 柯里化
柯里化 柯里化是把接受多个参数的函数,变成接受一个单一参数的函数.并且返回接受剩余参数和返回结果的新函数. 就是一个逐次消元的过程. 当把函数的元全消掉,就得到了值. 值就是零元函数. 二元函数 f( ...
- 使用百度网盘配置私有Git服务
GitHub上免费的版本只能开源代码库,有时候需要配置些私有的服务,不方便公开.现在免费的网盘的容量越来越大,可以用来做存储的服务,如果只使用网盘存储合并代码很不方便,所以使用网盘+git 配置私有仓 ...
- Linked dylibs built for GC-only but object files built for retain/release for architecture x86_64
编译开源Xcode 插件 SCXcodeSwitchExpander 源码地址: https://github.com/stefanceriu/SCXcodeSwitchExpander 编译环境:X ...