/*
最小费用最大流,增广路径用基于bellmanford算法的queue改进版本
邻接矩阵实现
*/
// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <strstream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>
using namespace std;
// typedef
typedef long long LL;
typedef unsigned long long ULL;
//
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FF(i,a) for(int i=0;i<(a);i+++)
#define FFD(i,a) for(int i=(a)-1;i>=0;i--)
#define Z(a) (a<<1)
#define Y(a) (a>>1)
const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
if(x>y) return x;
return y;
}
template<class T> inline T TMIN(T x,T y)
{
if(x<y) return x;
return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
T t = x;
x = y;
y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
return TMAX(TMAX(x,y),z);
}
// code begin
#define MAXN 220
class MinCostMaxFlow_fordfulkerson_SPFA_queue
{
private:
int G[MAXN][MAXN]; //剩余网络
int C[MAXN][MAXN]; //费用网络
int N,M,source,sink;
// SPFA用
int dist[MAXN];
int isin[MAXN];
int cnt[MAXN];
int fat[MAXN]; //aug用
int que[MAXN]; //BFS用
public:
MinCostMaxFlow_fordfulkerson_SPFA_queue(){}
void Set(int Nt,int sourcet,int sinkt)
{
N = Nt;
source = sourcet;
sink = sinkt;
}
void Init()
{
memset(G,0,sizeof(G));
FORi(1,N+1,1)
{
FORj(1,N+1,1)
C[i][j] = INFi;
}
}
void Add_graph_edge(int a,int b,int c)
{
G[a][b] = c;
}
void Add_cost_edge(int a,int b,int c)
{
C[a][b] = c;
C[b][a] = -c;
}
void Print()
{
FORi(1,N+1,1)
{
FORj(1,N+1,1)
{
printf("%d ",C[i][j]);
}
printf("\n");
}
printf("\n");
}
public:
int SPFA() // bellman ford queue optimization
{
// from source
fill(dist,dist+N+1,INFi);
memset(isin,0,sizeof(isin));
memset(cnt,0,sizeof(cnt));
int head(0),tail(0);
dist[source] = 0;
que[tail] = source;
tail = (tail+1)%MAXN;
isin[source] = true;
cnt[source] ++;
while(head!=tail)
{
int cur = que[head];
isin[cur] = false;
head = (head+1)%MAXN;
//printf("%d %d\n",cur,dist[cur]);
FORi(1,N+1,1)
{
if( G[cur][i]!=0 && dist[cur]+C[cur][i]<dist[i])
{
dist[i] = dist[cur] + C[cur][i];
fat[i] = cur;
if(!isin[i])
{
isin[i] = true;
cnt[i] ++;
que[tail] = i;
tail = (tail+1)%MAXN;
}
}
}
}
if(dist[sink]==INFi) return -1;
return dist[sink];
}
int Augment()
{
int minp = INFi;
int cur = sink;
while(cur!=source)
{
if( G[fat[cur]][cur]<minp )
{
minp = G[fat[cur]][cur];
}
cur = fat[cur];
}
cur = sink;
while(cur!=source)
{
G[fat[cur]][cur] -= minp;
G[cur][fat[cur]] += minp;
cur = fat[cur];
}
return minp;
}
int mincostmaxflow()
{
int cost(0);
int flow(0);
int fdx;
int cdx;
while((cdx=SPFA())!=-1)
{
fdx = Augment();
cost += cdx*fdx;
flow += fdx;
}
return cost;
}
};
int N,source,sink;
int Row,Col;
struct node
{
int r;
int c;
};
node X[MAXN];
node Y[MAXN];
int xs;
int ys;
MinCostMaxFlow_fordfulkerson_SPFA_queue g;
int main()
{
read;
write;
char in[MAXN];
while(scanf("%d %d",&Row,&Col)!=-1)
{
if(Row+Col==0) break;
xs = 0;
ys = 0;
FORi(0,Row,1)
{
scanf("%s",in);
FORj(0,Col,1)
{
if(in[j]=='m')
{
X[xs].r = i;
X[xs].c = j;
xs++;
}
else if(in[j]=='H')
{
Y[ys].r = i;
Y[ys].c = j;
ys++;
}
}
}
source = xs+ys+1;
sink = xs+ys+2;
N = xs+ys+2;
g.Set(N,source,sink);
g.Init();
// 建图网络
FORi(1,xs+1,1)
{
//G[source][i] = 1;
g.Add_graph_edge(source,i,1);
}
FORi(1,xs+1,1)
{
FORj(1,ys+1,1)
{
//G[i][j+xs] = 1;
g.Add_graph_edge(i,j+xs,1);
}
}
FORi(1,ys+1,1)
{
//G[i+xs][sink] = 1;
g.Add_graph_edge(i+xs,sink,1);
}
// 建费用网络
FORi(1,xs+1,1)
{
//C[source][i] = 0;
g.Add_cost_edge(source,i,0);
}
FORi(1,xs+1,1)
{
FORj(1,ys+1,1)
{
//C[i][j+xs] = abs( X[i-1].c-Y[j-1].c ) + abs( X[i-1].r-Y[j-1].r );
//C[j+xs][i] = -C[i][j+xs];
g.Add_cost_edge(i,j+xs,abs( X[i-1].c-Y[j-1].c ) + abs( X[i-1].r-Y[j-1].r ));
}
}
FORi(1,ys+1,1)
{
//C[xs+i][sink] = 0;
g.Add_cost_edge(xs+i,sink,0);
}
// 一切就绪
printf("%d\n",g.mincostmaxflow());
}
return 0;
}
转载于:https://www.cnblogs.com/ac2012/archive/2011/03/17/1987573.html