Going Home

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
————————————————————————————————————
题目的意思是给出一张图,H表示房子,m表示人,人只能上下左右移动一格且花费为1,问所有的人进入房子花费最少是多少?
思路:
方法一:最小费最大流。建图时将每个人和每个房子两两之间建边,流量为1花费为人与房的曼哈顿距离。再加一个源点与每个人建边流量为1花费为0,一个汇点与每个房子建边流量为1花费为0,求源点到汇点的最小花费即可。
方法二:二分图最大权匹配,根据距离关系建立二分图。KM算法解决
方法一:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
using namespace std;
const int MAXN=10000;
const int MAXM=100000;
const int INF=0x3f3f3f3f;
struct Edge{
int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
struct point{
int x,y;
};
void init(int n)
{
N=n;
tol=0;
memset(head,-1,sizeof head);
}
void addedge(int u,int v,int cap,int cost)
{
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].flow=0;
edge[tol].cost=cost;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].flow=0;
edge[tol].cost=-cost;
edge[tol].next=head[v];
head[v]=tol++;
} bool spfa(int s,int t)
{
queue<int>q;
for(int i=0;i<N;i++)
{
dis[i]=INF;
vis[i]=false;
pre[i]=-1;
}
dis[s]=0;
vis[s]=true;
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)
{
int v=edge[i].to;
if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=i;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
if(pre[t]==-1)return false;
return true;
}
int MincostMaxflow(int s,int t)
{
int flow=0;
int cost=0;
while(spfa(s,t))
{
int Min=INF;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].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;
}
return cost;
} int main()
{
char mp[105][105];
int m,n;
while(~scanf("%d%d",&n,&m)&&(m||n))
{
point H[105],P[105];
int h=0,p=0;
for(int i=0;i<n;i++)
{
scanf("%s",&mp[i]);
for(int j=0;j<m;j++)
{
if(mp[i][j]=='H')
{
H[h].x=i;
H[h].y=j;
h++;
}
else if(mp[i][j]=='m')
{
P[p].x=i;
P[p].y=j;
p++;
}
}
}
init(p+h+2);
for(int i=0;i<h;i++)
for(int j=0;j<p;j++)
{
int c=fabs(H[i].x-P[j].x)+fabs(H[i].y-P[j].y);
addedge(i+1,h+j+1,1,c);
} for(int i=0;i<h;i++)
{
addedge(0,i+1,1,0);
}
for(int i=0;i<p;i++)
{
addedge(h+1+i,h+p+1,1,0);
}
printf("%d\n",MincostMaxflow(0,h+p+1)); }
}

方法二:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF=0x3f3f3f3f;
const int MAXN = 505;
int g[MAXN][MAXN];
int lx[MAXN],ly[MAXN]; //顶标
int linky[MAXN];
int visx[MAXN],visy[MAXN];
int slack[MAXN];
char mp[MAXN][MAXN];
int nx,ny;
bool find(int x)
{
visx[x] = true;
for(int y = 0; y < ny; y++)
{
if(visy[y])
continue;
int t = lx[x] + ly[y] - g[x][y];
if(t==0)
{
visy[y] = true;
if(linky[y]==-1 || find(linky[y]))
{
linky[y] = x;
return true; //找到增广轨
}
}
else if(slack[y] > t)
slack[y] = t;
}
return false; //没有找到增广轨(说明顶点x没有对应的匹配,与完备匹配(相等子图的完备匹配)不符)
} int KM() //返回最优匹配的值
{
int i,j;
memset(linky,-1,sizeof(linky));
memset(ly,0,sizeof(ly));
for(i = 0; i < nx; i++)
for(j = 0,lx[i] = -INF; j < ny; j++)
lx[i] = max(lx[i],g[i][j]);
for(int x = 0; x < nx; x++)
{
for(i = 0; i < ny; i++)
slack[i] = INF;
while(true)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(find(x)) //找到增广轨,退出
break;
int d = INF;
for(i = 0; i < ny; i++) //没找到,对l做调整(这会增加相等子图的边),重新找
{
if(!visy[i] && d > slack[i])
d = slack[i];
}
for(i = 0; i < nx; i++)
{
if(visx[i])
lx[i] -= d;
}
for(i = 0; i < ny; i++)
{
if(visy[i])
ly[i] += d;
else
slack[i] -= d;
}
}
}
int result = 0;
for(i = 0; i < ny; i++)
if(linky[i]>-1)
result += g[linky[i]][i];
return result;
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)&&(n||m))
{
for(int i=0; i<n; i++)
{
scanf("%s",mp[i]);
}
int cnt=0;
int CNT=0;
memset(g,-INF,sizeof g);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(mp[i][j]=='m')
{
int CNT=0;
for(int I=0; I<n; I++)
for(int J=0; J<m; J++)
{
if(mp[I][J]=='H')
{
g[cnt][CNT++]=-(abs(i-I)+abs(j-J));
}
}
cnt++;
} }
nx=ny=cnt;
printf("%d\n",-KM());
}
return 0;
}

POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏的更多相关文章

  1. c++ 字符串流 sstream(常用于格式转换) 分类: C/C++ 2014-11-08 17:20 150人阅读 评论(0) 收藏

    使用stringstream对象简化类型转换 C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中 ...

  2. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  3. POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...

  5. 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...

  6. 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏

    #include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...

  7. POJ2195 Going Home[费用流|二分图最大权匹配]

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22088   Accepted: 11155 Desc ...

  8. 【BZOJ 3308】 3308: 九月的咖啡店 (费用流|二分图最大权匹配)

    3308: 九月的咖啡店 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 244  Solved: 86 Description 深绘里在九份开了一家咖 ...

  9. [hdu1533]二分图最大权匹配 || 最小费用最大流

    题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...

随机推荐

  1. 在linux下利用nohup来后台运行java程序

    nohup java -jar /etc/deny/denyHttp_fat.jar & http://limaoyuan.iteye.com/blog/900928 http://zshou ...

  2. thinkpad t420安装debian需要注意的细节

    关闭双显卡,使用集成显卡,32位可以用独显跑起来,但是64位的wheezy只能兼容集成显卡,不知道为啥,否则会在某些usb插入/或者拔出来以后,重启提示 failed to execute /lib/ ...

  3. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  4. Fri Oct 31 18:00:00 UTC+0800 2008转换为yyyy-mm-dd

    这个其实网上有很多例子,都是直接用js在前端做了时间处理,我的处理也一样,想要变成2008-3-31,就用下面的js直接可以处理 function Todate(num) { //Fri Oct 31 ...

  5. CentOS6.5 安装mysql-5.7.9

    转自:http://forrest-lv.iteye.com/blog/2260703 安装前,需要检查是否已经有mysql服务进程,是否已经装过mysql;  这点很重要,我之前安装CentOS的同 ...

  6. MySQL创建函数报“ERROR 1418 ”错误,不能创建函数

    MySQL创建函数报ERROR 1418错误,不能创建函数,根据官方提示是说,不能创建函数可能是一个安全设置方面的配置或功能未开启原因,下面我们一起来看.   错误 ERROR 1418 (HY000 ...

  7. 现象级AR营销助力“口碑双十二”,蚂蚁特工在全国数万商户掀起“AR捉四宝”

    领取阅读奖励金 今年双十二,全国人民吃喝玩乐放飞自我,嗨出了新纪元.除了见证你国人民的财力,这个“双十二”还诞生了教科书级的“AR营销”.无论是在口碑商户门口,还是在各大购物广场,都能看到举着手机,正 ...

  8. 1.ehcache实现页面整体缓存和页面局部缓存

    转自:https://www.cnblogs.com/jianjianyang/p/4933016.html 好长时间没写博客了,真的是没时间啊.ps:其实就是懒!!接下来几篇要写下缓存,这里主要写下 ...

  9. 用Msxml2.XMLHTTP 与 Msxml2.ServerXMLHTTP 发生网页请求

    发送 HTTP 请求时,首先想到的一般是 Msxml2.XMLHTTP(Microsoft.XMLHTTP 已经不提倡使用了). ServerXMLHTTP 为不同 Web 服务器之间的服务器安全 H ...

  10. How do I list subversion repository's ignore settings

    If it is Windows and you are using TortoiseSVN, then right-click on a folder of the working copy, go ...