Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24716 Accepted: 12383
Description
[外链图片转存失败(img-UU4j6l4s-1564220523439)(http://poj.org/images/2195_1.jpg)]
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
题意简述:平面内有相同数量的人和房子,一个人走一格需要 1 1 1的花费,求人和房子全部匹配所需要的最小花费
不难发现如果把人和房子分成两个集合,将人和房子的曼哈顿距离看作是两两之间连上的边的权值的话,这题实际上是要求这个二分图的最小匹配,那么我们建一个源点和汇点,然后按如下的方式连边再跑最小费用流即可。
从 s s s到人:容量为 1 1 1,权值为 0 0 0。
从人到房子:容量为 1 1 1,权值为两者的曼哈顿距离。
从房子到 t t t:容量为 t t t,权值为 0 0 0
代码如下:
#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;
}