Going Home (bipartite graph matching)

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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int INF=0x3f3f3f3f;
const int maxn=10100;
char mp [110] [110];


struct M{
    int x,y;
    M(){}
    M(int _x,int _y):x(_x),y(_y){}
}mm[maxn];
struct H{
    int x,y;
    H(){}
    H(int _x,int _y):x(_x),y(_y){}
}hh[maxn];

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){}
};

struct MM{
    int n,m;
    vector<Edge> edge;
    vector<int> G[maxn];
    int inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
    void init(int n){
        this->n=n;
        for(int i=0;i<n;i++) G[i].clear();
        edge.clear();
    }

    void add_edge(int u,int v,int cap,int cost){
        edge.push_back(Edge(u,v,cap,0,cost));
        edge.push_back(Edge(v,u,0,0,-cost));
        m=edge.size();
        G[u].push_back(m-2);
        G[v].push_back(m-1);
    }
    bool spfa(int s,int t,int &flow,ll& cost){
        for(int i=0;i<n;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;
            int sz=G[u].size();
            for(int i=0;i<sz;i++){
                Edge& e=edge[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=edge[p[u]].from){
            edge[p[u]].flow+=a[t];
            edge[p[u]^1].flow-=a[t];
        }
        return true;
    }
    int MinMax(int s,int t,ll &cost){
        int flow=0;cost=0;
        while(spfa(s,t,flow,cost));
        return flow;
    }
}mcmf;


intmain()
{
    int n,m;
    while(scanf("%d %d",&n,&m)==2&&n&&m){
        int cm=0,ch=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                scanf(" %c",&mp[i][j]);
                if(mp[i][j]=='m')
                    mm[++cm]=M(i,j);
                if(mp[i][j]=='H')
                    hh[++ch]=H(i,j);
            }
        }
        mcmf.init(cm+ch+2);
        int st=0,ed=cm+ch+1;
        for(int i=1;i<=cm;i++){
            mcmf.add_edge(st,i,1,0);
            mcmf.add_edge(i+cm,ed,1,0);
            for(int j=1;j<=ch;j++){
                int v=cm+j,u=i;
                mcmf.add_edge(u,v,1,abs(mm[i].x-hh[j].x)+abs(mm[i].y-hh[j].y));
            }
        }
        ll ans;
        mcmf.MinMax(st,ed,ans);
        printf("%I64d\n",ans);
    }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325823245&siteId=291194637