Going Home
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6954 Accepted Submission(s): 3668
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.
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
思路:找出人和屋子的位置,距离,建立二分图跑KM算法。
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 300010 #define maxn 500005 #define REP(i, a, b) for(int i = (a); i <= (b); ++ i) #define REP(j, a, b) for(int j = (a); j <= (b); ++ j) #define PER(i, a, b) for(int i = (a); i >= (b); -- i) using namespace std; template <class T> inline void rd(T &ret){ char c; ret = 0; while ((c = getchar()) < '0' || c > '9'); while (c >= '0' && c <= '9'){ ret = ret * 10 + (c - '0'), c = getchar(); } } const int inf=0x3f3f3f; int q[110][110],slack[110],visx[110],visy[110],n,m,dis[110][110],tot,mh[110],h[110],s[110]; char p[110][110]; struct node{ int x,y; }u[110],v[110]; int dfs(int cur){ visx[cur]=1; for(int i=1;i<=tot;i++){ if(visy[i])continue; int now=h[cur]+s[i]-q[cur][i]; if(!now){ visy[i]=1; if(mh[i]==-1||dfs(mh[i])){ mh[i]=cur; return 1; } } else if(slack[i]>now)slack[i]=now; } return 0; } int KM(){ memset(h,-inf,sizeof(h)); memset(mh,-1,sizeof(mh)); memset(s,0,sizeof(s)); for(int i=1;i<=tot;i++){ for(int j=1;j<=tot;j++){ h[i]=max(h[i],q[i][j]); } } for(int i=1;i<=tot;i++){ for(int j=1;j<=tot;j++)slack[j]=inf; while(1){ memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); if(dfs(i))break; int now=inf; for(int j=1;j<=tot;j++){ if(!visy[j]&&now>slack[j])now=slack[j]; } for(int j=1;j<=tot;j++){ if(visx[j])h[j]-=now; } for(int j=1;j<=tot;j++){ if(visy[j])s[j]+=now; else slack[j]-=now; } } } int ans=0; for(int i=1;i<=tot;i++){ if(mh[i]>-1)ans+=q[mh[i]][i]; } return ans; } int main(){ while(cin>>n>>m){ if(m+n==0)break; for(int i=1;i<=n;i++)scanf("%s",p[i]+1); int o=0,r=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(p[i][j]=='m')u[++o].x=i,u[o].y=j; if(p[i][j]=='H')v[++r].x=i,v[r].y=j; } } tot=o; for(int i=1;i<=o;i++){ for(int j=1;j<=r;j++){ q[i][j]=-abs(u[i].x-v[j].x)-abs(u[i].y-v[j].y); } } cout<<-KM()<<endl; } return 0; }