1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <iostream> #include <queue> #include <cstring>
using namespace std; const int inf = 1e9+5; const int maxn = 1e5+5;
struct Edge { int to,next,cost,c,flow; } edge[maxn];
int head[maxn],tot; int pre[maxn],dist[maxn]; bool vis[maxn]; char ch[110][110]; int x[210],y[210]; int n,m;
void addedge(int u,int v,int c,int cost) { edge[tot].to = v; edge[tot].c = c; edge[tot].cost = cost; edge[tot].flow = 0; edge[tot].next = head[u]; head[u] = tot++; edge[tot].to = u; edge[tot].c = 0; edge[tot].cost = -cost; edge[tot].flow = 0; edge[tot].next = head[v]; head[v] = tot++; }
bool spfa(int source,int sink) { queue<int> que; memset(dist,inf,sizeof dist); memset(vis,0,sizeof vis); memset(pre,-1,sizeof pre); dist[source] = 0; vis[source] = 1; que.push(source); while (!que.empty()) { int cur = que.front(); que.pop(); vis[cur] = false; for (int i=head[cur]; i!=-1; i=edge[i].next) { int v=edge[i].to; if (edge[i].c>edge[i].flow && dist[v]>dist[cur]+edge[i].cost) { dist[v] = dist[cur] + edge[i].cost; pre[v] = i; if (!vis[v]) { vis[v] = 1; que.push(v); } } } } if (pre[sink] == -1) return false; return true; }
void Dinic(int source,int sink,int &cost) { cost = 0; while (spfa(source,sink)) { int delta = inf; int i = pre[sink]; while (i != -1) { delta = min(delta,edge[i].c - edge[i].flow); i = pre[edge[i^1].to]; } i = pre[sink]; while (i != -1) { edge[i].flow += delta; edge[i^1].flow -= delta; cost += edge[i].cost * delta; i = pre[edge[i^1].to]; } } }
int main() { while (scanf("%d%d",&n,&m) && (n+m)) { for (int i=0; i<n; ++i) scanf("%s",ch[i]); int num=0; for (int i=0; i<n; ++i) for (int j=0; j<m; ++j) if (ch[i][j] == 'm') { ++num; x[num] = i; y[num] = j; } int cnt = 0; for (int i=0; i<n; ++i) for (int j=0; j<m; ++j) if (ch[i][j] == 'H') { ++cnt; x[num+cnt] = i; y[num+cnt] = j; } int source=0,sink=num+cnt+1; memset(head,-1,sizeof head); tot = 0; for (int i=1; i<=num; ++i) addedge(source,i,1,0); for (int i=num+1; i<=num+cnt; ++i) addedge(i,sink,1,0); for (int i=1; i<=num; ++i) for (int j=1; j<=cnt; ++j) { int u = i,v = num+j; int c = abs(x[u]-x[v]) + abs(y[u] - y[v]); addedge(u,v,1,c); } int cost; Dinic(source,sink,cost); printf("%d\n",cost); } return 0; }
|