Going Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21120 | Accepted: 10668 |
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.
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.
![](http://img.e-com-net.com/image/info8/74689e5e874d4c81a2b48d7585138211.jpg)
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
题意:m表示人,H表示房子,一个人只能进一个房子,一个房子也只能进去一个人,房子数等于人数,现在要让所有人进入房子,求所有人都进房子最短的路径。
思路1:在哈工大出版的图论及应用上有这个原题,是出在二分图的区域,那本书上的模板有bug,我的博客有改过之后的正确模板:
KM算法模板
这题就是个匹配的问题,有多少人回家,让回家的路总和最短,这就让匹配有了权值,所以得用KM算法,不过KM算法是最优匹配,匹配得出来的是最大值,然而我们要求出来最小值,所以我们用一个最大值减去每条边的值,使大小顺序倒过来,然后跑完KM算法之后用最大值*n-匹配值就是得出的结果了。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int inf=1e9,maxn=110;
int tu[maxn][maxn],match1[maxn],match2[maxn];
int KM(int m,int n)
{
int s[maxn],t[maxn],l1[maxn],l2[maxn],p,q,ret=0,i,j,k;
///l1为左边的匹配分量,l2是右边的匹配分量
for(i=0; il1[i]?tu[i][j]:l1[i];
if(l1[i]==-inf)
return -1;
}
for(i=0; i=0; j=p)
match2[j]=k=t[j],p=match1[k],match1[k]=j;
}
}
if(match1[i]<0)
{
for(i--,p=inf,k=0; k<=q; k++)
for(j=0; jhou;
vectorman;
int real(int x)
{
if(x<0)
return -x;
return x;
}
int main()
{
int m,n;
while(~scanf("%d%d",&n,&m)&&m+n)
{
hou.clear();
man.clear();
for(int i=0; i
思路2:这题由于是要求总的最短路径,那么我们可以想到用最小费用流来解决这个问题。
我的博客有最小费用流的模板:最小费用流模板
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int oo=1e9;
const int mm=11111111;
const int mn=888888;
int node,src,dest,edge;
int ver[mm],flow[mm],cost[mm],nex[mm];
int head[mn],dis[mn],p[mn],q[mn],vis[mn];
/**这些变量基本与最大流相同,增加了cost 表示边的费用,p记录可行流上节点对应的反向边*/
void prepare(int _node,int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0; i=mn)?l=0:l)
for(i=head[u=q[l]],vis[u]=0; i>=0; i=nex[i])
if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i]))
{
dis[v]=tmp;
p[v]=i^1;
if(vis[v]) continue;
vis[q[r++]=v]=1;
if(r>=mn)r=0;
}
return p[dest]>-1;
}
int SpfaFlow()/**源点到汇点的一条最短路即可行流,不断的找这样的可行流*/
{
int i,ret=0,delta;
while(spfa())
{
for(i=p[dest],delta=oo; i>=0; i=p[ver[i]])
if(flow[i^1]=0; i=p[ver[i]])
flow[i]+=delta,flow[i^1]-=delta;
ret+=delta*dis[dest];
}
return ret;
}
struct hh
{
int x,y;
hh(int xx,int yy)
{
x=xx,y=yy;
}
};
vectorhou;
vectorman;
int real(int x)
{
if(x<0)
return -x;
return x;
}
char x[110][110];
int main()
{
int m,n;
while(~scanf("%d%d",&n,&m)&&n+m)
{
hou.clear();
man.clear();
for(int i=0; i