HDU-1533 Going Home(二分圖匹配)
阿新 • • 發佈:2019-01-27
最近開始做最小費用流的題目,該題是二分圖完美匹配下的最小權匹配,所謂完美匹配就是說從源點流入的總流量等於從匯點流出的總流量,在這種狀態下的最小費用 。
那麼顯然是要套用最小費用流模板,另外二分圖匹配的第一步就是要劃分集合,劃分兩個集合,集合A與源點相連,集合B與匯點相連,至於容量和權值就要依據題目而定 。
比如該題,因為每個小人恰好能對應一個房子,所以每個小人與匯點的容量為1,房子與匯點的容量為1,這樣就保證了是完美匹配。 那麼下一步要建立兩個集合中元素之間的關係,那麼容量顯然是可以隨便賦值的,因為每個房子到匯點的容量是1,已經限制了 。 另外每個人到每個房子的最短步數顯然就是權值 。那麼就很容易建圖了 。
最小費用流演算法用了最短路的Bellman演算法,不過這個Edmonds-Karp演算法貌似不是很快,時間複雜度O(v^2*E) 。
細節參見程式碼:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 200 + 5; const int INF = 1000000000; int T,n,t,m,a[maxn],p[maxn],d[maxn],inq[maxn],mm,hh; struct Edge { int from, to, cap, flow, cost; Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w) {} }; vector<Edge> edges; vector<int> g[maxn]; void init() { for(int i=0;i<maxn;i++) g[i].clear(); edges.clear(); } void AddEdge(int from, int to, int cap, int cost) { edges.push_back(Edge(from,to,cap,0,cost)); edges.push_back(Edge(to,from,0,0,-cost)); t = edges.size(); g[from].push_back(t-2); g[to].push_back(t-1); } bool BellmanFord(int s,int t,int& flow, ll& cost) { for(int i=0;i<maxn;i++) d[i] = INF; memset(inq,0,sizeof(inq)); d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; queue<int> Q; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); inq[u] = 0; for(int i = 0; i < g[u].size(); i++) { Edge& e = edges[g[u][i]]; if(e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost ; p[e.to] = g[u][i]; a[e.to] = min(a[u],e.cap - e.flow); if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; } } } } if(d[t] == INF) return false; flow += a[t]; cost += (ll)d[t] *(ll)a[t]; for(int u = t; u != s; u = edges[p[u]].from) { edges[p[u]].flow += a[t]; edges[p[u]^1].flow -= a[t]; } return true; } int MincostMaxflow(int s,int t, ll& cost) { int flow = 0; cost = 0; while(BellmanFord(s,t,flow,cost)) ; return flow; } struct node { int r,c; node(int r=0,int c=0): r(r),c(c) {} bool operator < (const node& v) const { return r < v.r || (r == v.r && c < v.c); } }M[maxn],H[maxn]; char s[105][105]; int main() { while(~scanf("%d%d",&n,&m)) { if( !n && !m ) return 0; init() ; for(int i=1;i<=n;i++) scanf("%s",s[i]+1); mm = hh = 1; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(s[i][j] == 'm') { M[mm].r = i; M[mm++].c = j; } else if(s[i][j] == 'H') { H[hh].r = i; H[hh++].c = j; } } int tt = hh+mm+2; for(int i=1;i<mm;i++) AddEdge(0,i,1,0); for(int i=1;i<hh;i++) AddEdge(i+mm,tt,1,0); for(int i=1;i<mm;i++) { for(int j=1;j<hh;j++) { int v = abs(M[i].r-H[j].r) + abs(M[i].c-H[j].c); AddEdge(i,j+mm,INF,v); } } ll ans = 0; int cnt = MincostMaxflow(0,tt,ans); printf("%I64d\n",ans); } return 0; }