poj 2195 Going Home

Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23292   Accepted: 11736

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

题意:有n个人要进n个屋子,每个屋子只能住一人,且每个人从初始位置起每走一步就要耗费1单位时间,耗费最短的时间让所有人进屋子。
思路:指派问题,最小费用最大流。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<functional>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
const int N_MAX = 100+4, V_MAX = 200+10;

typedef pair<int, int>P;
struct edge {
    int to, cap, cost, rev;
    edge(int to = 0, int cap = 0, int cost = 0, int rev = 0) :to(to), cap(cap), cost(cost), rev(rev) {}
};
int V;
vector<edge>G[V_MAX];
int h[V_MAX];
int dist[V_MAX];
int prevv[V_MAX], preve[V_MAX];

void add_edge(int from, int to, int cap, int cost) {
    G[from].push_back(edge(to, cap, cost, G[to].size()));
    G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}
//没流量则返回-1
int min_cost_flow(int s, int t, int f) {
    int res = 0;
    fill(h, h + V, 0);
    while (f>0) {
        priority_queue<P, vector<P>, greater<P> >que;
        fill(dist, dist + V, INF);
        dist[s] = 0;
        que.push(P(0, s));
        while (!que.empty()) {
            P p = que.top(); que.pop();
            int v = p.second;
            if (dist[v] < p.first)continue;
            for (int i = 0; i < G[v].size(); i++) {
                edge&e = G[v][i];
                if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
                    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                    prevv[e.to] = v;
                    preve[e.to] = i;
                    que.push(P(dist[e.to], e.to));
                }
            }
        }
        if (dist[t] == INF)return -1;
        for (int v = 0; v < V; v++)h[v] += dist[v];
        int d = f;
        for (int v = t; v != s; v = prevv[v]) {
            d = min(d, G[prevv[v]][preve[v]].cap);
        }
        f -= d;
        res += d*h[t];
        for (int v = t; v != s; v = prevv[v]) {
            edge&e = G[prevv[v]][preve[v]];
            e.cap -= d;
            G[v][e.rev].cap += d;
        }
    }
    return res;
}

void clear() {
    for (int i = 0; i < V; i++) {
        G[i].clear();
    }
}
int N, M;
struct position {
    int x ,y;
    position(int x=0,int y=0):x(x),y(y) {}
};
vector<position> pos_h, pos_m;
int dis[N_MAX][N_MAX];

int main() {
    while (scanf("%d%d",&N,&M)&&N) {
        pos_h.clear(), pos_m.clear();
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++) {
                char c;
                scanf(" %c", &c);
                if (c == 'm') { pos_m.push_back(position(i, j)); }
                if (c == 'H') { pos_h.push_back(position(i, j)); }
            }
        int num = pos_m.size();
        for (int i = 0; i <num;i++) {//人i到房j的距离
            for (int j = 0; j < num;j++) {
                dis[i][j] = abs(pos_m[i].x - pos_h[j].x) + abs(pos_m[i].y-pos_h[j].y);
            }
        }
        int s = 2 * num, t = s + 1;
        V = t + 1;
        //0~num-1:人
        //num~2num-1:房子
        for (int i = 0; i < num;i++) {
            add_edge(s,i,1,0);
        }
        for (int i = 0; i < num;i++) {
            add_edge(num+i,t,1,0);
        }
        for (int i = 0; i <num; i++) {
            for (int j = 0; j < num; j++) {
                add_edge(i,num+j,1,dis[i][j]);
              }
            }
        printf("%d
",min_cost_flow(s,t,num));
        clear();
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/ZefengYao/p/7281272.html