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.

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;
}
全部评论

相关推荐

吐泡泡的咸鱼:我也工作了几年了,也陆陆续续面试过不少人,就简历来说,第一眼学历不太够,你只能靠你的实习或者论文或者项目经历,然后你没有论文,没有含金量高的比赛和奖项,只能看实习和项目,实习来说,你写的实习经历完全不清楚你想找什么工作?行研?数据分析?且写的太少了,再看项目,这些项目先不说上过大学读过研究生的都知道很水,然后对你想找的岗位有什么帮助呢?项目和实习也完全不匹配啊,你好像在努力将你所有的经历都放在简历里想表现你的优秀,但是对于你想找的岗位来说,有什么用呢?最后只能获得岗位不匹配的评价。所以你需要明白你想要找的岗位要求是什么,是做什么的,比如产品经理,然后再看你的经历里有什么匹配的上这个岗位,或者对这个岗位以及这个岗位所在的公司有价值,再写到你的简历上
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务