D - Going Home ( 最小费用最大流 )

烈酒焚心 提交于 2019-11-29 23:44:16

D - Going Home ( 最小费用最大流 )

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
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

题意:m代表人, H代表房子, 每个m都要到达H,每个H只能容纳一个m, 求所有m到达H的最小花费( 走过的距离 )。

思路:最小费用最大流, 设超级源点连接所有m, 流量为1, 权重为0。 所有H连接超级汇点,流量为1,权重为0。 所有m和所有H连接, 流量为1,权重为距离。

注:需要将二位地图抽象为一个图,首先需要将m和H编号, 题意说m和H在2~100内, 所以m从1编号, H从102开始编号,这样就不会冲突。

代码:

#include <iostream>
#include <math.h>
#include <cmath>
#include <queue>
#include <cstring>
#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 1e5+10;
int n,m,s,t,maxflow,mincost;
int pre[maxn],via[maxn],dis[maxn],last[maxn],flow[maxn];
int head[maxn];
char a[105][105];
struct node {
    int to,w,f,nxt;
} e[maxn];
struct nod {
    int x,y;
} M[maxn],H[maxn];
int cnt1,cnt2,cnt;

void addage( int u, int v, int f, int w )
{
    e[cnt].to = v;
    e[cnt].f = f;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

int spfa()
{
    memset(dis,inf,sizeof(dis));
    memset(flow,inf,sizeof(flow));
    memset(via,0,sizeof(via));
    queue <int> Q;
    Q.push(s); dis[s]=0; via[s]=1; pre[t] = -1;
    while ( !Q.empty() ) {
        int x = Q.front(); Q.pop(); via[x] = 0;
        for ( int i=head[x]; i!=-1; i=e[i].nxt ) {
            int y=e[i].to, f=e[i].f, w=e[i].w;
            if ( f && dis[y]>dis[x]+w ) {
                dis[y] = dis[x] + w;
                pre[y] = x;
                flow[y] = min( flow[x], f );
                last[y] = i;
                if ( via[y]==0 ) {
                    Q.push(y); via[y] = 1;
                }
            }
        }
    }
    return pre[t]!=-1;
}

void MFML()
{
    maxflow = mincost = 0;
    while ( spfa() ) {
        maxflow += flow[t];
        mincost += flow[t]*dis[t];
        int x = t;
        while ( x!=s ) {
            e[last[x]].f -= flow[x];
            e[last[x]^1].f += flow[x];
            x = pre[x];
        }
    }
    cout << mincost << endl;
}

int dist( nod x, nod y )
{
    return ( abs(x.x-y.x) + abs(x.y-y.y) );
}

int main()
{
    int i,j;
    while ( cin>>n>>m ) {
        if ( n==0&&m==0 ) break ;
        cnt1=1; cnt2 = 102; cnt = 0;
        s = 0, t = 210;
        memset(head,-1,sizeof(head));
        for ( i=0; i<n; i++ ) {
            for ( j=0; j<m; j++ ) {
                cin >> a[i][j];
                if ( a[i][j]=='m' ) {
                    M[cnt1].x = i;
                    M[cnt1].y = j;
                    addage(s,cnt1,1,0);
                    addage(cnt1,s,0,0);
                    cnt1 ++;
                }
                if ( a[i][j]=='H' ) {
                    H[cnt2].x = i;
                    H[cnt2].y = j;
                    addage(t,cnt2,0,0);
                    addage(cnt2,t,1,0);
                    cnt2 ++;
                }
            }
        }
        for ( i=1; i<cnt1; i++ ) {
            for ( j=102; j<cnt2; j++ ) {
                addage( i,j, 1, dist(M[i],H[j]) );
                addage( j, i, 0, -dist(M[i],H[j]) );
            }
        }
        MFML();
    }

    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!