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

Going Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21918 Accepted: 11087

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

Source

Pacific Northwest 2004

题意:在一个N*M的矩阵中,有n个人和房子,每个人每走一步的费用为1,每个房子只能住1个人,问所有人都住进房子的最小花费是多少

题解:最小费用最大流,把第1~n个人编号为1~n,第1~n个房子编号为1+n~2n,设源点和汇点编号分别为0和2n+1,将源点与1~n连一条费用为0容量为1的有向边,将n+1~2n与汇点连一条费用为0容量为1的有向边,将i∈[1,n]与j∈[n+1,2n]连一条费用为|x2-x1|+|y2-y1|容量为1的有向边.①费用不难理解,人与房子间的费用肯定等于两者间的最短距离;②为什么边的容量为1?注意每个房子只能住1个人,也就对应着该点的入度和出度等于1.

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=a-1;i>=b;--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'
#define eps 1e-10
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
namespace MCMF {
const int MX = 205;
const int ME = 4e5 + 5;//边的数量
int tot, n;
int st, en;//源点和汇点
int maxflow, mincost;
bool vis[MX];
int head[MX], cur[MX], dis[MX];

queue <int> Q;
struct Edge {
    int v, cap, cost, nxt, flow;
} E[ME], SE[ME];

void init(int _n) {
    n = _n, tot = 0;
    for(int i = 0; i <= n; i++) head[i] = -1;
}
void edge_add(int u, int v, int cap, int cost) {
    E[tot] = (Edge) {v, cap, cost, head[u], 0};
    head[u] = tot++;
    E[tot] = (Edge) {u, 0, -cost, head[v], 0};
    head[v] = tot++;
}
bool adjust() {
    int v, min = INF;
    for(int i = 0; i <= n; i++) {
        if(!vis[i]) continue;
        for(int j = head[i]; ~j; j = E[j].nxt) {
            v = E[j].v;
            if(E[j].cap - E[j].flow) {
                if(!vis[v] && dis[v] - dis[i] + E[j].cost < min) {
                    min = dis[v] - dis[i] + E[j].cost;
                }
            }
        }
    }

    if(min == INF) return 0;
    for(int i = 0; i <= n; i++) {
        if(vis[i]) {
            cur[i] = head[i];
            vis[i] = 0;
            dis[i] += min;
        }
    }
    return true;
}
int augment(int i, int flow) {
    if(i == en) {
        mincost += dis[st] * flow;
        maxflow += flow;
        return flow;
    }
    vis[i] = true;
    for(int j = cur[i]; j != -1; j = E[j].nxt) {
        int v = E[j].v;
        if(E[j].cap == E[j].flow) continue;
        if(vis[v] || dis[v] + E[j].cost != dis[i]) continue;
        int delta = augment(v, std::min(flow, E[j].cap - E[j].flow));
        if(delta) {
            E[j].flow += delta;
            E[j ^ 1].flow -= delta;
            cur[i] = j;
            return delta;
        }
    }
    return 0;
}
void spfa() {
    int u, v;
    for(int i = 0; i <= n; i++) {
        vis[i] = 0;
        dis[i] = INF;
    }
    Q.push(st);
    dis[st] = 0; vis[st] = 1;
    while(!Q.empty()) {
        u = Q.front(), Q.pop(); vis[u] = 0;
        for(int i = head[u]; ~i; i = E[i].nxt) {
            v = E[i].v;
            if(E[i].cap == E[i].flow || dis[v] <= dis[u] + E[i].cost) continue;
            dis[v] = dis[u] + E[i].cost;
            if(!vis[v]) {
                vis[v] = 1;
                Q.push(v);
            }
        }
    }
    for(int i = 0; i <= n; i++) {
        dis[i] = dis[en] - dis[i];
    }
}
int zkw(int s, int t, int &ret_flow) {
    st = s, en = t;
    spfa();
    mincost = maxflow = 0;
    for(int i = 0; i <= n; i++) {
        vis[i] = 0;
        cur[i] = head[i];
    }
    do {
        while(augment(st, INF)) {
            memset(vis, 0, n * sizeof(bool));
        }
    } while(adjust());
    ret_flow = maxflow;
    return mincost;
}
}
const int MX = 205;
vector<PII> v1, v2;
char st[MX];
int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    int n, m;
    while(scanf("%d%d", &n, &m), n + m) {
        v1.clear(); v2.clear();
        for(int i = 0; i < n; i++) {
            scanf("%s", st);
            for(int j = 0; j < m; j++) {
                if(st[j] == 'm') v1.push_back(PII(i, j));
                else if(st[j] == 'H') v2.push_back(PII(i, j));
            }
        }
        n = v1.size();
        MCMF::init(2 * n + 1);
        for(int i = 0; i < n; i++) {
            MCMF::edge_add(0, i + 1, 1, 0);
            MCMF::edge_add(i + n + 1, 2 * n + 1, 1, 0);
        }
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                int cost = abs(v1[i].x - v2[j].x) + abs(v1[i].y - v2[j].y);
                MCMF::edge_add(i + 1, j + n + 1, 1, cost);
            }
        }
        int f;
        printf("%d\n", MCMF::zkw(0, 2 * n + 1, f));
    }

#ifdef local
    //cout << "time cost:" << clock() << "ms" << endl;
#endif // local
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值