【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)
題干:
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 0Sample Output
2 10 28題目大意:
在n*m的矩陣格子里有x個人('m')要走到x個房間('H')里,每個房間里只能放一人,人在目前所在的格子處向上下左右相鄰的格子走一步就花費1美元,問最后全部的人走到房間后最小的花費。
解題報告:
? 直接建圖之后最小費用流。
新建超級源點st,超級匯點ed。
st連向每一個m,流量為1,費用為0。
每一個H連向ed,
新增源點from和匯點to,from->人(w為1,cost為0)
房子->to(w為1,cost為0)
每個人->每間房(w為1,cost為最短路徑的美元花費)
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long using namespace std;const int MAXN = 70000; const int MAXM = 100005; const int INF = 0x3f3f3f3f; struct Edge {int to,next,cap,flow,cost; } edge[MAXM]; int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N;//節點總個數,節點編號從 1 ~ N void init(int n) {N = n;tol = 0;memset(head, -1,sizeof(head)); } void addedge(int u,int v,int cap,int cost) {edge[tol].to = v;edge[tol].cap = cap;edge[tol].cost = cost;edge[tol].flow = 0;edge[tol].next = head[u];head[u] = tol++;edge[tol].to = u;edge[tol].cap = 0;edge[tol].cost = -cost;edge[tol].flow = 0;edge[tol].next = head[v];head[v] = tol++; } bool spfa(int s,int t) {queue<int>q;for(int i = 1; i <= N; i++) {dis[i] = INF;vis[i] = false;pre[i] = -1;}dis[s] = 0;vis[s] = true;q.push(s);while(!q.empty()) {int u = q.front();q.pop();vis[u] = false;for(int i = head[u]; i !=-1; i = edge[i].next) {int v = edge[i].to;if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ) {dis[v] = dis[u] + edge[i].cost;pre[v] = i;if(!vis[v]) {vis[v] = true;q.push(v);}}}}if(pre[t] ==-1)return false;else return true;} //返回的是最大流,cost 存的是最小費用 int minCostMaxflow(int s,int t,int &cost) {int flow = 0;cost = 0;while(spfa(s,t)) {int Min = INF;for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {if(Min > edge[i].cap-edge[i].flow)Min = edge[i].cap-edge[i].flow;}for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {edge[i].flow += Min;edge[i^1].flow-= Min;cost += edge[i].cost * Min;}flow += Min;}return flow; } char s[111][111]; int r[111],c[111]; int R[111],C[111]; int tot1,tot2; int main() {int n,m;while(~scanf("%d%d",&n,&m)) {if(n == 0 && m == 0) break;init(n*m+2);memset(dis,0,sizeof dis);tot1=tot2=0;int st=n*m+1,ed=n*m+2;int cost;for(int i = 1; i<=n; i++) {scanf("%s",s[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(s[i][j] == 'm') tot1++,r[tot1] = i,c[tot1] = j;if(s[i][j] == 'H') tot2++,R[tot2] = i,C[tot2] = j;}}for(int i = 1; i<=tot1; i++) {for(int j = 1; j<=tot2; j++) addedge(i,tot1+j,1,abs(c[i]-C[j]) + abs(r[i]-R[j]));}for(int i = 1; i<=tot1; i++) addedge(st,i,1,0);for(int i = 1; i<=tot2; i++) addedge(tot1+i,ed,1,0);int ans = minCostMaxflow(st,ed,cost);printf("%d\n",cost);} return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 男子乘网约车提出开空调被拒载!司机:打车
- 下一篇: 零百加速3秒级!华为徐直军:第一次见阿维