Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3800    Accepted Submission(s): 1948

Problem 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
 
Recommend
lwg   |   We have carefully selected several similar problems for you:  1532 3416 3491 3338 3081

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 200+10
#define MAXM 80000+100
#define INF 0x3f3f3f
struct node
{
int from,to,cap,flow,cost,next;
}edge[MAXM];
int head[MAXN],top,pre[MAXN];
int dis[MAXN],vis[MAXN],n,m,source,sink;
void init()
{
top=0;
memset(head,-1,sizeof(head));
}
struct Node
{
int x,y;
};
Node H[MAXN],M[MAXN];
int m_cnt,h_cnt;
void add(int u,int v,int w,int c)
{
node E1={u,v,w,0,c,head[u]};
edge[top]=E1;
head[u]=top++;
node E2={v,u,0,0,-c,head[v]};
edge[top]=E2;
head[v]=top++;
}
int dist(int x1, int y1, int x2, int y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
void getmap()
{
m_cnt=h_cnt=0;
char str[110][110];
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
for(int j=0;j<m;j++)
{
if(str[i][j]=='m')
{
++m_cnt;
M[m_cnt].x=i;
M[m_cnt].y=j;
}
if(str[i][j]=='H')
{
h_cnt++;
H[h_cnt].x=i;
H[h_cnt].y=j;
}
}
}
int k=m_cnt;
source=0;
sink=2*k+1;
for(int i=1;i<=k;i++)
{
add(source,i,1,0);
add(i+k,sink,1,0);
for(int j=1;j<=k;j++)
{
int d=dist(H[i].x,H[i].y,M[j].x,M[j].y);
add(i,j+k,1,d);
}
}
}
bool SPFA(int s,int t)
{
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.to]>dis[u]+E.cost&&E.cap>E.flow)
{
dis[E.to]=dis[u]+E.cost;
pre[E.to]=i;
if(!vis[E.to])
{
vis[E.to]=true;
q.push(E.to);
}
}
}
}
return pre[t]!=-1;
}
void MCMF(int s,int t,int &cost,int &flow)
{
flow=0;
cost=0;
while(SPFA(s,t))
{
int Min=INF;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
node E=edge[i];
Min=min(Min,E.cap-E.flow);
}
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
// printf("%d\n",cost);
}
}
int main()
{
while(scanf("%d%d",&n,&m),n|m)
{
init();
getmap();
int cost,flow;
MCMF(source,sink,cost,flow);
printf("%d\n",cost);
}
return 0;
}

最新文章

  1. Web性能优化-合并js与css,减少请求
  2. mybatis 的if else
  3. wifipineapple使用教程
  4. 导入DXF文件
  5. Adele的生活
  6. maven3 junit4 spring3 jdk8 :junit一直报错,害的我几个星期都是这个错,你妹的!
  7. Notepad++编辑Pyhton文件的自动缩进的问题(图文)
  8. 基础语法 swift
  9. 表单javascript checkbox全选 反选 全不选
  10. 【Flume NG用户指南】(1)设置
  11. C++出现计算机术语5
  12. css(二) block,inline和inline-block概念和区别
  13. ABP入门系列(12)——如何升级Abp并调试源码
  14. TCP/IP读书笔记(4) IPv4和IPv6 路由选择
  15. iOS学习——核心动画之Layer基础
  16. word图片自动编号,前面加章节号
  17. web漏洞扫描工具AWVS使用
  18. 数据存储(直接写入、NSUserDefaults、NSkeyedArchiver)
  19. Linux学习笔记-基本操作4
  20. 配置redis, make的时候: zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录

热门文章

  1. jQuery学习笔记之概念(1)
  2. ASP.NET Cache 实现依赖Oracle的缓存策略
  3. AI:狄拉克之海上的涟漪
  4. Caffe: gflag编译出现问题汇总
  5. 安卓Java读取SD卡文本文件
  6. 实体服务器安装centos7过程记录
  7. Linux思维导图之用户、组和权限
  8. 搭建rsync服务(端口号873)
  9. SpringBoot 整合 Mybatis 进行CRUD测试开发
  10. Django-xadmin+django-import-export导入导出的实现