POJ 2195 Going Home(最小费用最大流)

来源:互联网 发布:智能家居服务器源码 编辑:程序博客网 时间:2024/06/08 11:09

Description

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

最小费用最大流的裸题。建图的时候有一点小技巧:每个m和H之间的最短距离(即所需的钱数)等于横坐标之差的绝对值加上纵坐标之差的绝对值,不管你怎么走过去。建好图后加一个源点和一个汇点,便转化为mcmf的裸题。代码如下:

#include<algorithm>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<cmath>using namespace std;int n,m,s = 0,t = 20000 + 1;const int maxn = 20000 + 5;const int INF = 1 << 30;const int la = 100 + 5;int l,r;//m数和M数int d[maxn],a[maxn],p[maxn];bool vis[maxn];typedef struct point{    int x,y;    point(int xx,int yy):x(xx),y(yy){}}point;typedef struct node{    int from,to,cap,flow,cost;    node(int f,int tt,int c,int ff,int cc):from(f),to(tt),cap(c),flow(ff),cost(cc){}}node;vector<point> in,out;vector<int> G[maxn];vector<node> edge;void Init(){    for(int i = 0; i < maxn; ++i) G[i].clear();    in.clear(),out.clear();    l = r = 0;}void addedge(int from,int to,int cap,int flow,int cost){    edge.push_back(node(from,to,cap,flow,cost));    edge.push_back(node(to,from,0,0,-cost));    int mm = edge.size();    G[from].push_back(mm-2);    G[to].push_back(mm-1);}bool SPFA(int &flow,int &cost){    memset(vis,false,sizeof(vis));    for(int i = 1;i <= maxn; ++i) d[i] = INF;//Be carefull!    queue<int> q;    q.push(s);    a[s] = INF,vis[s] = true,d[s] = 0;    while(!q.empty())    {        int x = q.front();q.pop();        vis[x] = false;        for(int i = 0;i < G[x].size(); ++i)        {            node &e = edge[G[x][i]];            if(e.cap > e.flow && d[e.to] > d[x] + e.cost)            {                d[e.to] = d[x] + e.cost;                p[e.to] = G[x][i];//Be carefull !                a[e.to] = min(a[x],e.cap - e.flow);//Be carefull !                if(!vis[e.to]) { q.push(e.to);vis[e.to] = true; }            }        }    }    if(d[t] == INF) return false;//Becarefull !    flow += a[t];    cost += d[t];//Be carefull !    int x = t;    while(x != s)    {        edge[p[x]].flow += a[t];        edge[p[x]^1].flow -= a[t];        x = edge[p[x]].from;    }    return true;}int mcmf(){    int flow = 0,cost = 0;    while(SPFA(flow,cost));    return cost;}int main(){    while(scanf("%d %d",&n,&m) && (n || m))    {        getchar();        Init();        for(int i = 0;i < n; ++i)        {            for(int j = 0;j < m; ++j)            {                char p = getchar();                if(p == 'm') in.push_back(point(i,j)), ++l;                else if(p == 'H') out.push_back(point(i,j)), ++r;            }            getchar();        }        for(int i = 1;i <= l; ++i)        {            addedge(s,i,1,0,0);//sorce to m            addedge(i + 10000,t,1,0,0);//H to sink        }        for(int i = 1;i <= l; ++i)        {            for(int j = 1;j <= r; ++j)            {                int cos = abs(in[i-1].x - out[j-1].x) + abs(in[i-1].y - out[j-1].y);                addedge(i,j + 10000,1,0,cos);            }        }        printf("%d\n",mcmf());    }    return 0;}

0 0
原创粉丝点击