Going Home
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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
Sample Output
21028
———————————————————————————————————— 題目的意思是給出一張圖,H表示房子,m表示人,人只能上下左右移動一格且花費為1,問所有的人進入房子花費最少是多少。 思路: 方法一:最小費最大流。建圖時將每個人和每個房子兩兩之間建邊,流量為1花費為人與房的曼哈頓距離。再加一個源點與每個人建邊流量為1花費為0,一個匯點與每個房子建邊流量為1花費為0,求源點到匯點的最小花費即可。 方法二:二分圖最大權匹配,根據距離關係建立二分圖。KM演算法解決 方法一:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;#define MAXN 60100#define MAXM 1000100int vis[MAXN],d[MAXN],pre[MAXN],a[MAXN];struct Edge{int u, v, c, cost, next;} edge[MAXM];int s[MAXN], cnt;void init(){cnt = 0;memset(s, -1, sizeof(s));}void add(int u, int v, int c, int cost){edge[cnt].u = u;edge[cnt].v = v;edge[cnt].cost = cost;edge[cnt].c = c;edge[cnt].next = s[u];s[u] = cnt++;edge[cnt].u = v;edge[cnt].v = u;edge[cnt].cost = -cost;edge[cnt].c = 0;edge[cnt].next = s[v];s[v] = cnt++;}bool spfa(int ss, int ee,int &flow,int &cost){queue<int> q;memset(d, INF, sizeof d);memset(vis, 0, sizeof vis);d[ss] = 0, vis[ss] = 1, pre[ss] = 0, a[ss] = INF;q.push(ss);while (!q.empty()){int u = q.front();q.pop();vis[u] = 0;for (int i = s[u]; ~i; i = edge[i].next){int v = edge[i].v;if (edge[i].c>0&& d[v]>d[u] + edge[i].cost){d[v] = d[u] + edge[i].cost;pre[v] = i;a[v] = min(a[u], edge[i].c);if (!vis[v]){vis[v] = 1;q.push(v);}}}}if (d[ee] == INF) return 0;flow += a[ee];cost += d[ee]*a[ee];int u = ee;while (u != ss){edge[pre[u]].c -= a[ee];edge[pre[u] ^ 1].c += a[ee];u = edge[pre[u]].u;}return 1;}int MCMF(int ss, int ee){int cost = 0, flow=0;while (spfa(ss, ee, flow, cost));return cost;}struct point{int x,y;};int main(){ char mp[105][105]; int m,n; while(~scanf("%d%d",&n,&m)&&(m||n)) { point H[105],P[105]; int h=0,p=0; for(int i=0;i<n;i++) { scanf("%s",&mp[i]); for(int j=0;j<m;j++) { if(mp[i][j]=='H') { H[h].x=i; H[h].y=j; h++; } else if(mp[i][j]=='m') { P[p].x=i; P[p].y=j; p++; } } } init(); for(int i=0;i<h;i++) for(int j=0;j<p;j++) { int c=fabs(H[i].x-P[j].x)+fabs(H[i].y-P[j].y); add(i+1,h+j+1,1,c); } for(int i=0;i<h;i++) { add(0,i+1,1,0); } for(int i=0;i<p;i++) { add(h+1+i,h+p+1,1,0); } printf("%d\n",MCMF(0,h+p+1)); }return 0;}
方法二:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;const int MAXN = 505;int g[MAXN][MAXN];int lx[MAXN],ly[MAXN]; //頂標int linky[MAXN];int visx[MAXN],visy[MAXN];int slack[MAXN];char mp[MAXN][MAXN];int nx,ny;bool find(int x){ visx[x] = true; for(int y = 0; y < ny; y++) { if(visy[y]) continue; int t = lx[x] + ly[y] - g[x][y]; if(t==0) { visy[y] = true; if(linky[y]==-1 || find(linky[y])) { linky[y] = x; return true; //找到增廣軌 } } else if(slack[y] > t) slack[y] = t; } return false; //沒有找到增廣軌(說明頂點x沒有對應的匹配,與完備匹配(相等子圖的完備匹配)不符)}int KM() //返回最優匹配的值{ int i,j; memset(linky,-1,sizeof(linky)); memset(ly,0,sizeof(ly)); for(i = 0; i < nx; i++) for(j = 0,lx[i] = -INF; j < ny; j++) lx[i] = max(lx[i],g[i][j]); for(int x = 0; x < nx; x++) { for(i = 0; i < ny; i++) slack[i] = INF; while(true) { memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); if(find(x)) //找到增廣軌,退出 break; int d = INF; for(i = 0; i < ny; i++) //沒找到,對l做調整(這會增加相等子圖的邊),重新找 { if(!visy[i] && d > slack[i]) d = slack[i]; } for(i = 0; i < nx; i++) { if(visx[i]) lx[i] -= d; } for(i = 0; i < ny; i++) { if(visy[i]) ly[i] += d; else slack[i] -= d; } } } int result = 0; for(i = 0; i < ny; i++) if(linky[i]>-1) result += g[linky[i]][i]; return result;}int main(){ int n,m; while(~scanf("%d%d",&n,&m)&&(n||m)) { for(int i=0; i<n; i++) { scanf("%s",mp[i]); } int cnt=0; int CNT=0; memset(g,-INF,sizeof g); for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(mp[i][j]=='m') { int CNT=0; for(int I=0; I<n; I++) for(int J=0; J<m; J++) { if(mp[I][J]=='H') { g[cnt][CNT++]=-(abs(i-I)+abs(j-J)); } } cnt++; } } nx=ny=cnt; printf("%d\n",-KM()); } return 0; }