POJ 2195 Going Home (minimum cost maximum flow) __ Graph theory

Source: Internet
Author: User
Tags abs
Description

On a grid map there are n little men and N houses. In each of the unit's time, every little mans can move one unit of step, either horizontally, or vertically, to a adjacent point. For each little mans, you need to pay a $ travel fee for every step he moves, until he enters a house. The task is complicated, with the restriction, can accommodate only one little mans.

Your task is to compute the minimum amount of, need to pay in order to send this n little men into those n Diffe Rent houses. The input is a map of the scenario, a '. ' means a empty spaces, an ' H ' represents a house ', and am ' m ' indica TES There is a little mans on this point.

You can have a point on the grid map as a quite large square and so it can hold n little men in the same time; Also, it is okay if a little mans steps on a grid with a house without entering.


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 was the number of rows of the map, and M is the number of Columns. The rest of the input would be N lines describing the map. You may assume both N and M are between 2 and inclusive. There would be the same number of ' H ' s and ' m ' in the map; And there'll is at most houses. Input would terminate with 0 0 for N and M.


Output

For the all test case, output one, and the single integer, which are the minimum amount, in dollars, and 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 0


Sample Output

2
28


the

Give a map,. Is the open space, H is the house, M is a villain, and the map has the same number of houses and villains, each person can only be horizontal or vertical movement of a lattice, and finally everyone to find a separate house to walk the number of steps.


train of Thought

Fortunately, there is no wall on the map (reduce the difficulty), the characters can also meet in the same lattice, then the villain distance to any one of the room's step is that they are the difference between the horizontal and the ordinate.

Find a source point, and it's seen with all the villains there's a 1-cost side, then all the houses and meeting points also have a capacity for 1 of the cost for 0 of the side, the villain and the side of the house capacity for 1, the cost of the distance between them (step number).

Of course, the reverse mapping can also be, because this picture is symmetrical. And the number of houses and the number of villains are equal.

Then find the minimum cost from the source point to the meeting point.


AC Code

#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <
Math.h> #include <iostream> using namespace std;
#include <queue> #include <map> const int MAXX = 10005;

const int INF = 0X3F3F3F3F;     struct Edge {int to;   Edge endpoint int next;    Next sibling position int cap;   capacity int flow;   flow int cost;

Cost} edge[maxx<<2];
int head[maxx],tol;
int Pre[maxx],dis[maxx];  int N;
    Total number of nodes void init (int n) {n=n;
    tol=0;
Memset (head) (head,-1,sizeof);
    } void Addedge (int u,int v,int cap,int cost)//increase both the original edge and the reverse edge {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++;
    /* * SPFA algorithm to determine if there is a path of s to T/bool SPFA (int s,int t) {queue<int>q;
    BOOL Vis[maxx]; for (int i=0; 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)//traverse all point {int v=edge[i].to with u; if (edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)//If you can relax the point {dis[v]=
                Dis[u]+edge[i].cost;
                Pre[v]=i;
                    if (!vis[v])//If the point is not in the queue, team {vis[v]=true;
                Q.push (v);    }} return (Pre[t]!=-1);
    Returns whether S to T has a path}/* int s start * int t endpoint * * int mincostmaxflow (int s,int t,int &cost) {int flow=0;   while (SPFA (s,t)) {int minn=inf;
    The current path can be augmented with a value for (int i=pre[t]; i!=-1 i=pre[edge[i^1].to])//Because each additional edge of the graph is added to its reverse side at the same time, so i^1 to find the exact opposite part of I {        if (Minn>edge[i].cap-edge[i].flow) Minn=edge[i].cap-edge[i].flow;
            for (int i=pre[t]; i!=-1 i=pre[edge[i^1].to])//modify diagram to calculate cost {Edge[i].flow+=minn;
            Edge[i^1].flow-=minn;
        Cost+=edge[i].cost*minn;
    } Flow+=minn;
} return flow;
    } struct Node {int x;
    int y;
        Node () {} node (int x,int y) {this->x=x;
    this->y=y;

}
};

inline int Getlen (node A,node b) {return abs (a.x-b.x) +abs (A.Y-B.Y);}
    int main () {int n,m;
    Char str[105][105]; while (~SCANF ("%d%d%*c", &n,&m) && (n| |   m)) {node h[5005],m[5005];
        analog stack int th=0,tm=0;
            for (int i=0; i<n; i++) {gets (str[i]);  for (int j=0; j<m; J + +) {if (str[i][j]== ' H ') H[th++]=node (I,J); House else if (str[i][j]== ' m ') M[tm++]=node (i,j);
        Villain}} init (th+tm+2);       for (int i=0; i<th; i++) {Addedge (th+tm,i,1,0);    Source point to house for (int j=0; j<tm; j + +) Addedge (I,th+j,1,getlen (h[i],m[j));  House to villain} for (int i=0; i<tm; i++) Addedge (th+i,th+tm+1,1,0);
        Villain to the meeting point int cost=0;
        Mincostmaxflow (Th+tm,th+tm+1,cost);
    printf ("%d\n", cost);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.