1. 程式人生 > >POJ 2195 Going Home(最小費用最大流)

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

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.
img


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

題目連結

建圖:源點與人建立流量為1(1個人)費用為0(顯然)的邊,匯點與房屋建立流量為1(一個房屋只能進一個人),費用為0的邊(顯然),將每個人與每個房屋之間建立流量為1,費用為人與房屋之間的曼哈頓距離的邊,跑最小費用最大流即可。

AC程式碼:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
const int INF = 0x3f3f3f3f;
const int maxn = 5e3 + 5;

struct Edge {
    int V, Next, Cap, Flow, Cost;
    Edge(int _V = 0, int _Next = 0, int _Cap = 0, int _Flow = 0, int _Cost = 0): V(_V), Next(_Next), Cap(_Cap), Flow(_Flow), Cost(_Cost) {}
};

int Head[maxn];
int Path[maxn];
int Dis[maxn];
bool Vis[maxn];
int Tot;
Edge edges[maxn];

int N, M;
char maze[maxn][maxn];

void Init() {
    Tot = 0;
    memset(Head, -1, sizeof(Head));
}

void AddEdge(int U, int V, int Cap, int Cost) {
    edges[Tot].V = V;
    edges[Tot].Cap = Cap;
    edges[Tot].Cost = Cost;
    edges[Tot].Flow = 0;
    edges[Tot].Next = Head[U];
    Head[U] = Tot++;
    edges[Tot].V = U;
    edges[Tot].Cap = 0;
    edges[Tot].Cost = -Cost;
    edges[Tot].Flow = 0;
    edges[Tot].Next = Head[V];
    Head[V] = Tot++;
}

bool SPFA(int Start, int End) {
    memset(Dis, INF, sizeof(Dis));
    memset(Vis, false, sizeof(Vis));
    memset(Path, -1, sizeof(Path));
    Dis[Start] = 0;
    Vis[Start] = true;
    std::queue<int> Que;
    while (!Que.empty()) {
        Que.pop();
    }
    Que.push(Start);
    while (!Que.empty()) {
        int U = Que.front();
        Que.pop();
        Vis[U] = false;
        for (int i = Head[U]; i != -1; i = edges[i].Next) {
            int V = edges[i].V;
            if (edges[i].Cap > edges[i].Flow && Dis[V] > Dis[U] + edges[i].Cost) {
                Dis[V] = Dis[U] + edges[i].Cost;
                Path[V] = i;
                if (!Vis[V]) {
                    Vis[V] = true;
                    Que.push(V);
                }
            }
        }
    }
    return Path[End] != -1;
}

int MinCostMaxFlow(int Start, int End, int &MinCost) {
    int MaxFlow = 0;
    MinCost = 0;
    while (SPFA(Start, End)) {
        int Min = INF;
        for (int i = Path[End]; i != -1; i = Path[edges[i ^ 1].V]) {
            if (edges[i].Cap - edges[i].Flow < Min) {
                Min = edges[i].Cap - edges[i].Flow;
            }
        }
        for (int i = Path[End]; i != -1; i = Path[edges[i ^ 1].V]) {
            edges[i].Flow += Min;
            edges[i ^ 1].Flow -= Min;
            MinCost += edges[i].Cost * Min;
        }
        MaxFlow += Min;
    }
    return MaxFlow;
}

int main(int argc, char *argv[]) {
    while (~scanf("%d%d", &N, &M) && N + M) {
        Init();
        std::vector<std::pair<int, int> > Man, House;
        for (int i = 1; i <= N; ++i) {
            scanf("%s", maze[i]);
            for (int j = 1; j <= M; ++j) {
                if (maze[i][j - 1] == 'm') {
                    Man.push_back(std::make_pair(i, j));
                }
                else if (maze[i][j - 1] == 'H') {
                    House.push_back(std::make_pair(i, j));
                }
            }
        }
        for (int i = 1; i <= int(Man.size()); ++i) {
            AddEdge(0, i, 1, 0);
            for (int j = 1; j <= int(House.size()); ++j) {
                if (i == 1) {
                    AddEdge(int(Man.size()) + j, int(Man.size()) + int(House.size()) + 1, 1, 0);
                }
                AddEdge(i, int(Man.size()) + j, 1, abs(Man[i - 1].first - House[j - 1].first) + abs(Man[i - 1].second - House[j - 1].second));
            }
        }
        int MinCost;
        MinCostMaxFlow(0, int(Man.size()) + int(House.size()) + 1, MinCost);
        printf("%d\n", MinCost);
    }
    return 0;
}