消停套模板,没啥好说de
#include <stdio.h>
#include <string.h>
#define M 310
#define inf 0x3f3f3f3f
int abs(int x)
{
return x>0?x:-x;
}
int n,m,nx,ny;
int link[M],lx[M],ly[M],slack[M];///lx,ly为顶标,nx,ny分别为x点集y点集的个数
int visx[M],visy[M],w[M][M];
int DFS(int x)
{
visx[x] = 1;
for (int y = 1; y <= ny; y ++)
{
if (visy[y]) continue;
int t = lx[x] + ly[y] - w[x][y];
if (t == 0)
{
visy[y] = 1;
if (link[y] == -1||DFS(link[y]))
{
link[y] = x;
return 1;
}
}
else if (slack[y] > t) ///不在相等子图中slack 取最小的
slack[y] = t;
}
return 0;
}
int KM()
{
int i,j;
memset (link,-1,sizeof(link));
memset (ly,0,sizeof(ly));
for (i = 1; i <= nx; i ++) ///lx初始化为与它关联边中最大的
for (j = 1,lx[i] = -inf; j <= ny; j ++)
if (w[i][j] > lx[i])
lx[i] = w[i][j];
for (int x = 1; x <= nx; x ++)
{
for (i = 1; i <= ny; i ++)
slack[i] = inf;
while (1)
{
memset (visx,0,sizeof(visx));
memset (visy,0,sizeof(visy));
if (DFS(x)) ///若成功(找到了增广轨),则该点增广完成,进入下一个点的增广
break; ///若失败(没有找到增广轨),则需要改变一些点的标号,使得图中可行边的数量增加。
///方法为:将所有在增广轨中(就是在增广过程中遍历到)的X方点的标号全部减去一个常数d,
///所有在增广轨中的Y方点的标号全部加上一个常数d
int d = inf;
for (i = 1; i <= ny; i ++)
if (!visy[i]&&d > slack[i])
d = slack[i];
for (i = 1; i <= nx; i ++)
if (visx[i])
lx[i] -= d;
for (i = 1; i <= ny; i ++) ///修改顶标后,要把所有不在交错树中的Y顶点的slack值都减去d
if (visy[i])
ly[i] += d;
else
slack[i] -= d;
}
}
int res = 0;
for (i = 1; i <= ny; i ++)
if (link[i] > -1)
res += w[link[i]][i];
return res;
}
char a[104][104];
struct note
{
int x,y;
}node1[105],node2[105];
int main()
{
//freopen("cin.txt","r",stdin);
while(~scanf("%d%d",&n,&m)&&n&&m)
{
nx=ny=0;
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
for(int j=0;j<m;j++)
{
if(a[i][j]=='m') node1[++nx].x=i,node1[nx].y=j;
if(a[i][j]=='H') node2[++ny].x=i,node2[ny].y=j;
}
}
for(int i=1;i<=nx;i++)
{
for(int j=1;j<=ny;j++)
{
w[i][j]=-abs(node1[i].x-node2[j].x)-abs(node1[i].y-node2[j].y);
}
}
printf("%d\n",-KM());
}
return 0;
}