描述
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.
输入
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.
输出
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
样例输入
样例输出
题目来源
Pacific Northwest 2004
//最大权二分匹配 #include<stdio.h> #include<string> #include<cmath> int g[105][105];//邻接矩阵 int m[105][2];//存储人所在位置的坐标 int h[105][2];//存储房子所在位置的坐标 int lx[105],ly[105];//顶点标号 bool sx[105],sy[105];//是否已经搜索过 int link[105],n; char ss[105][105]; int min(int a,int b) { if(a<b) return a; return b; } bool path(int k)//从x[k]寻找增广路 { int i; sx[k]=true; for(i=0;i<n;i++) { if(!sy[i]&&(lx[k]+ly[i]==g[k][i])) { sy[i]=1; if(link[i]==-1||path(link[i])) { link[i]=k; return true; } } } return false; } int BestMatch()//求解最小权匹配 { int d,sum,i,j,k; memset(ly,0,sizeof(ly)); memset(link,-1,sizeof(link)); for(i=0;i<n;i++) { lx[i]=-1; //x中顶点i的编号为与i关联的Y中边的最大权重 for(j=0;j<n;j++) if(lx[i]<g[i][j])lx[i]=g[i][j]; } for( k=0;k<n;k++){ while(1){ memset(sx,0,sizeof(sx)); memset(sy,0,sizeof(sy)); if(path(k)) break;//匹配成功 d=10000000; for(i=0;i<n;i++){ if(sx[i]) for(j=0;j<n;j++){ if(!sy[j]) d=min(d,lx[i]+ly[j]-g[i][j]); } } for(i=0;i<n;i++){ if(sx[i]) lx[i]-=d; if(sy[i]) ly[i]+=d; } for(i=0;i<n;i++) { printf("%d %d/n",lx[i],ly[i]); } printf("/n"); } printf("/n"); } sum=0; for(i=0;i<n;i++) sum+=g[link[i]][i]; //注意这里本来是求的最大权匹配,这里取反即为最小权匹配(因为临街矩阵中的边的权值也取反了) return -sum; } int main() { int mi,hi,M,N,i,j; while(scanf("%d%d",&N,&M)!=EOF) { if(N==0&&M==0) break; mi=hi=0; for(i=0;i<N;i++) { scanf("%s",ss[i]); for(j=0;j<M;j++) { if(ss[i][j]=='m') { m[mi][0]=i; m[mi][1]=j; mi++; } else if(ss[i][j]=='H') { h[hi][0]=i; h[hi][1]=j; hi++; } } } n=mi; for(i=0;i<n;i++) { printf("%d %d/n",h[i][0],h[i][1]); } printf("/n"); for(i=0;i<n;i++) { printf("%d %d/n",m[i][0],m[i][1]); } for(i=0;i<n;i++){ for(j=0;j<n;j++){ g[i][j]=-(abs(m[i][0]-h[j][0])+abs(m[i][1]-h[j][1]));//即将边的权值取反 printf("%d ",g[i][j]); } printf("/n"); }printf("/n"); printf("%d/n",BestMatch()); } return 0; }