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.
![](https://i-blog.csdnimg.cn/blog_migrate/29da4c22cd9a35eb96b2a08b75ce8420.jpeg)
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
题解
费用流裸题。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define mod 210
#define inf 1<<30
using namespace std;
int n,m,H,R,hs,rs,T;
struct wu {int x,y;} ho[110],r[110];
char ch[105];
int zz,head[210];
struct bian{int frm,to,nx,v,c;} e[50002];
int dis[210],q[210],from[210],pd[210],ans=0;
int juli(int i,int j)
{return abs(r[i].x-ho[j].x)+abs(r[i].y-ho[j].y);}
void insert(int x,int y,int z,int c)
{
zz++; e[zz].to=y; e[zz].frm=x; e[zz].v=z;
e[zz].c=c; e[zz].nx=head[x]; head[x]=zz;
zz++; e[zz].to=x; e[zz].frm=y; e[zz].v=0;
e[zz].c=-c; e[zz].nx=head[y]; head[y]=zz;
}
void init()
{
int i,j;
hs=rs=0; zz=1;
memset(head,0,sizeof(head));
for(i=0;i<n;i++)
{scanf("%s",ch);
for(j=0;j<m;j++)
{if(ch[j]=='H')
{hs++; ho[hs].x=i; ho[hs].y=j;}
else if(ch[j]=='m')
{rs++; r[rs].x=i; r[rs].y=j;}
}
}
T=rs+hs+1;
for(i=1;i<=rs;i++) insert(0,i,1,0);
for(j=1;j<=hs;j++) insert(rs+j,T,1,0);
for(i=1;i<=rs;i++)
for(j=1;j<=hs;j++)
insert(i,rs+j,1,juli(i,j));
}
bool spfa()
{
int t=0,w=1,i,p,x;
memset(dis,127/3,sizeof(dis));
memset(pd,false,sizeof(pd));
memset(from,0,sizeof(from));
q[0]=dis[0]=0; pd[0]=1;
while(t!=w)
{x=q[t]; t=(t+1)%mod;
for(i=head[x];i;i=e[i].nx)
{p=e[i].to;
if(e[i].v&&dis[p]>dis[x]+e[i].c)
{dis[p]=dis[x]+e[i].c;
from[p]=i;
if(!pd[p]) {q[w]=p; pd[p]=1; w=(w+1)%mod;}
}
}
pd[x]=0;
}
if(dis[T]>100002) return false;
return true;
}
void mcf()
{
int i,x=inf;
i=from[T];
while(i)
{x=min(x,e[i].v); i=from[e[i].frm];}
i=from[T];
while(i)
{e[i].v-=x; e[i^1].v+=x; ans+=x*e[i].c; i=from[e[i].frm];}
}
int main()
{
while(scanf("%d%d",&n,&m)&&n&&m)
{init();
ans=0;
while(spfa()) mcf();
printf("%d\n",ans);
}
return 0;
}