HDU-1533 Going Home(二分图匹配)

来源:互联网 发布:复制别人店铺的软件 编辑:程序博客网 时间:2024/06/04 18:21

最近开始做最小费用流的题目,该题是二分图完美匹配下的最小权匹配,所谓完美匹配就是说从源点流入的总流量等于从汇点流出的总流量,在这种状态下的最小费用 。 

那么显然是要套用最小费用流模板,另外二分图匹配的第一步就是要划分集合,划分两个集合,集合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;}


1 1
原创粉丝点击