# poj2195（二分图最大匹配，最小费用流）

Going Home
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21120 Accepted: 10668

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.

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 <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;

const int inf=1e9,maxn=110;
int tu[maxn][maxn],match1[maxn],match2[maxn];
int KM(int m,int n)
{
int s[maxn],t[maxn],l1[maxn],l2[maxn],p,q,ret=0,i,j,k;
///l1为左边的匹配分量，l2是右边的匹配分量
for(i=0; i<m; i++)
{
for(l1[i]=-inf,j=0; j<n; j++)
l1[i]=tu[i][j]>l1[i]?tu[i][j]:l1[i];
if(l1[i]==-inf)
return -1;
}
for(i=0; i<n; l2[i++]=0);
memset(match1,-1,sizeof(int)*n);
memset(match2,-1,sizeof(int)*n);
for(i=0; i<m; i++)
{
memset(t,-1,sizeof(int)*n);
for(s[p=q=0]=i; p<=q&&match1[i]<0; p++)
{
for(k=s[p],j=0; j<n&&match1[i]<0; j++)
if(l1[k]+l2[j]==tu[k][j]&&t[j]<0)
{
s[++q]=match2[j],t[j]=k;
if(s[q]<0)
for(p=j; p>=0; j=p)
match2[j]=k=t[j],p=match1[k],match1[k]=j;
}
}
if(match1[i]<0)
{
for(i--,p=inf,k=0; k<=q; k++)
for(j=0; j<n; j++)
if(t[j]<0&&l1[s[k]]+l2[j]-tu[s[k]][j]<p)
p=l1[s[k]]+l2[j]-tu[s[k]][j];
for(j=0; j<n; l2[j]+=t[j]<0?0:p,j++);
for(k=0; k<=q; l1[s[k++]]-=p);
}
}
for(i=0; i<m; i++)
ret+=tu[i][match1[i]];
return ret;
}

char x[110][110];
struct hh
{
int x,y;
hh(int xx,int yy)
{
x=xx,y=yy;
}
};
vector<hh>hou;
vector<hh>man;

int real(int x)
{
if(x<0)
return -x;
return x;
}

int main()
{
int m,n;
while(~scanf("%d%d",&n,&m)&&m+n)
{
hou.clear();
man.clear();
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
scanf(" %c",&x[i][j]);
if(x[i][j]=='H')
{
hh t(i,j);
hou.push_back(t);
}
else if(x[i][j]=='m')
{
hh t(i,j);
man.push_back(t);
}
}
int l=hou.size();
for(int i=0; i<l; i++)
for(int j=0; j<l; j++)
tu[i][j]=100000-(real(hou[i].x-man[j].x)+real(hou[i].y-man[j].y));
printf("%d\n",100000*l-KM(l,l));
}
return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const   int oo=1e9;
const   int mm=11111111;
const   int mn=888888;
int node,src,dest,edge;
int ver[mm],flow[mm],cost[mm],nex[mm];
/**这些变量基本与最大流相同，增加了cost 表示边的费用，p记录可行流上节点对应的反向边*/
void prepare(int _node,int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0; i<node; i++)head[i]=-1,vis[i]=0;
edge=0;
}
void addedge(int u,int v,int f,int c)
{
}
bool spfa()/**spfa 求最短路，并用 p 记录最短路上的边*/
{
int i,u,v,l,r=0,tmp;
for(i=0; i<node; ++i)dis[i]=oo;
dis[q[r++]=src]=0;
p[src]=p[dest]=-1;
for(l=0; l!=r; (++l>=mn)?l=0:l)
if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i]))
{
dis[v]=tmp;
p[v]=i^1;
if(vis[v]) continue;
vis[q[r++]=v]=1;
if(r>=mn)r=0;
}
return p[dest]>-1;
}
int SpfaFlow()/**源点到汇点的一条最短路即可行流，不断的找这样的可行流*/
{
int i,ret=0,delta;
while(spfa())
{
for(i=p[dest],delta=oo; i>=0; i=p[ver[i]])
if(flow[i^1]<delta)delta=flow[i^1];
for(i=p[dest]; i>=0; i=p[ver[i]])
flow[i]+=delta,flow[i^1]-=delta;
ret+=delta*dis[dest];
}
return ret;
}

struct hh
{
int x,y;
hh(int xx,int yy)
{
x=xx,y=yy;
}
};
vector<hh>hou;
vector<hh>man;

int real(int x)
{
if(x<0)
return -x;
return x;
}
char x[110][110];
int main()
{
int m,n;
while(~scanf("%d%d",&n,&m)&&n+m)
{
hou.clear();
man.clear();
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
scanf(" %c",&x[i][j]);
if(x[i][j]=='H')
{
hh t(i,j);
hou.push_back(t);
}
else if(x[i][j]=='m')
{
hh t(i,j);
man.push_back(t);
}
}
int l=hou.size();
prepare(2*l+2,2*l,2*l+1);
for(int i=0; i<l; i++)
for(int j=0; j<l; j++)
for(int i=0; i<l; i++)