HDU-1533 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.
InputThere 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. 
OutputFor 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

最小费用最大流,感觉网络流的很多题目建图是难点。

#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>

#define MAX 500
#define INF 0x3fffffff

using namespace std;

typedef struct edge {
    int from;   // 起点
    int to;     // 终点
    int cap;    // 容量
    int flow;   // 流量
    int cost;   // 费用
    edge( int _from, int _to, int _cap, int _flow, int _cost ) {
        from = _from;
        to = _to;
        cap = _cap;
        cost = _cost;
        flow = _flow;
    }
} Edge;

vector<Edge> edges;
vector<int> MGraph[MAX];

int inq[MAX];   // 顶点i是否在队列中
int dist[MAX];  // 最短距离
int pre[MAX];     // 上一条弧
int add[MAX];     // 可改进量

void init() {
    for( int i = 0; i < MAX; i++ ) MGraph[i].clear();
    edges.clear();
    memset( pre, 0, sizeof( pre ) );
    memset( add, 0, sizeof( add ) );

}

bool spfa( int s, int t, int &flow, int &cost, int n ) {
    fill( dist, dist + MAX, INF );
    fill( inq, inq + MAX, 0 );

    dist[s] = 0;
    inq[s] = 1;
    pre[s] = 0;
    add[s] = INF;

    queue<int> q;
    q.push( s );

    while( !q.empty() ) {
        int u = q.front();
        q.pop();
        inq[u]--;
        for( int i = 0; i < MGraph[u].size(); i++ ) {
            Edge& e = edges[MGraph[u][i]];
            if( e.cap > e.flow && dist[e.to] > dist[u] + e.cost ) { //满足可增广且可变短
                dist[e.to] = dist[u] + e.cost;
                pre[e.to] = MGraph[u][i];
                add[e.to] = min( add[u], e.cap - e.flow );
                if( !inq[e.to] ) {
                    inq[e.to]++;
                    q.push( e.to );
                }
            }
        }
    }
    if( dist[t] == INF ) return false;  //汇点不可达则退出
    flow += add[t];
    cost += dist[t] * add[t];
    int u = t;
    while( u != s ) { //更新正向边和反向边
        edges[pre[u]].flow += add[t];
        edges[pre[u]^1].flow -= add[t];
        u = edges[pre[u]].from;
    }

    return true;
}

int min_cost_flow( int s, int t, int &min_cost, int n ) {
    int max_flow = 0;
    while( spfa( s, t, max_flow, min_cost, n ) );
    return max_flow;
}

void add_edge( 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 ) );
    int m = edges.size();
    MGraph[from].push_back( m - 2 );
    MGraph[to].push_back( m - 1 );
}

int buildMGraph( char input[MAX][MAX], int n, int m ) {
    int id[MAX][MAX];   // 顶点映射
    int k = 1;

    for( int i = 0; i < n; i++ ) {
        for( int j = 0; j < m; j++ ) {
            if( input[i][j] == 'm' || input[i][j] == 'H' ) {
                id[i][j] = k++;
            }
        }
    }

    int s = 0;  // 源点是0
    int t = k;  // 终点是t
    for( int i = 0; i < n; i++ ) {
        for( int j = 0; j < m; j++ ) {
            if( input[i][j] == 'm' ) {
                int u = id[i][j];
                add_edge( s, u, 1, 0 ); // 在源点和每个人之间建立一条边

                for( int _i = 0; _i < n; _i++ ) {
                    for( int _j = 0; _j < m; _j++ ) {
                        if( input[_i][_j] == 'H' ) {
                            int v = id[_i][_j];
                            // 在每个人和每个房子之间建立一条边
                            add_edge( u, v, 1, abs( _i - i ) + abs( _j - j ) );
                        }
                    }
                }
            }

            else if( input[i][j] == 'H' ) {
                add_edge( id[i][j], t, 1, 0 );  // 在每个房子和汇点建立一条边
            }
        }
    }

    return t;   // 返回终点编号
}

void printMGraph( int n ) {
    for( int i = 0; i < n; i++ ) {
        printf( "%d:\n", i );
        for( int j = 0; j < MGraph[i].size(); j++ ) {
            int e = MGraph[i][j];
            printf( "%d-%d\n", edges[e].from, edges[i].to );
        }
        printf( "\n" );
    }
}
int main() {
    int n, m;
    char input[MAX][MAX];

    while( scanf( "%d%d", &n, &m ) != EOF ) {
        if( n == 0 && m == 0 ) break;
        init();
        for( int i = 0; i < n; i++ ) scanf( "%s", input[i] );

        int s = 0;
        int t = buildMGraph( input, n, m );
        //printMGraph( t );
        int min_cost = 0;

        min_cost_flow( s, t, min_cost, t + 1 );

        printf( "%d\n", min_cost );
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值