POJ2195(最小费用最大流)

Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23761   Accepted: 11948

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


解题思路:费用流模板题,建图方式为超级源点到每一个房子连边,流量为1,费用为0,每个房子到每个人连边,流量为1,费用为这个的曼哈顿距离, 然后每个人到超级汇点连边,流量为1,费用为0,然后求一个源点到汇点的最小费用最大流就行。


#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <iostream>
#include <cstring>
using namespace std;
int inf = 0x3f3f3f3f;
const int maxm = (10000<<1);
const int maxn = 1000;
int N, M;
char Map[105][105];
int head[maxn];
int Maxflow;
int tot;
int num1, num2;//人和房子的数量
int src, sink;
int dist[maxn];
bool inQue[maxn];
int prev[maxn], pree[maxn];
vector<pair<int, int > > human;
vector<pair<int, int> > house;
struct edge{
    int v, w, f, nxt;
}Edge[maxm];
void init()
{
    num1 = num2 = tot = 0;
    Maxflow = 0;
    memset(head, -1, sizeof(head));
    human.clear();
    house.clear();
}
void add(int u, int v, int w, int f)
{
    Edge[tot].v = v;
    Edge[tot].w = w;
    Edge[tot].f = f;
    Edge[tot].nxt = head[u];
    head[u] = tot++;
    Edge[tot].v = u;
    Edge[tot].w = -w;
    Edge[tot].f = 0;
    Edge[tot].nxt = head[v];
    head[v] = tot++;
}
bool SPFA()//从源点到汇点进行最短路的增广
{
    memset(inQue, false, sizeof(inQue));
    memset(dist, inf, sizeof(dist));
    queue<int> que;
    while(!que.empty()) que.pop();
    que.push(src);
    inQue[src] = true;
    dist[src] = 0;
    while(!que.empty())
    {
        int u = que.front();
        que.pop();
        for(int i = head[u]; i != -1; i = Edge[i].nxt)
        {
            int v = Edge[i].v;
            int w = Edge[i].w;
            int f = Edge[i].f;
            if(f > 0 && dist[u] + w < dist[v])
            {
                dist[v] = dist[u] + w;
                prev[v] = u;
                pree[v] = i;
                if(!inQue[v])
                {
                    inQue[v] = true;
                    que.push(v);
                }
            }
        }
        inQue[u] = false;
    }
    if(dist[sink] < inf) return true;
    else return false;
}
int mincostflow()
{
    int ans = 0;
    while(SPFA())
    {
        int u = sink;
        int delta = inf;
        while(u != src)
        {
            int f = Edge[pree[u]].f;
            if(f < delta) delta = f;
            u = prev[u];
        }
        u = sink;
        while(u != src)
        {
            Edge[pree[u]].f -= delta;
            Edge[pree[u]^1].f += delta;
            u = prev[u];
        }
        Maxflow += delta;
        ans += delta * dist[sink];
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d", &N, &M) && N && M)
    {
        for(int i = 1; i <= N; i++)
        {
            scanf("%s", Map[i] + 1);
        }
        init();
        for(int i = 1; i <= N; i++)
        {
            for(int j = 1; j <= M; j++)
            {
                if(Map[i][j] == 'm')
                {
                    human.push_back(make_pair(i, j));
                }
                else if(Map[i][j] == 'H')
                {
                    house.push_back(make_pair(i, j));
                }
            }
        }
        num1 = human.size();
        num2 = house.size();
        src = 0;
        sink = num1 + num2 + 1;
        for(int i = 0; i < num1; i++)
        {
            add(src, i + 1, 0, 1);
        }
        for(int i = 0; i < num2; i++)
        {
            add(num1 + 1 + i, sink, 0, 1);
        }
        for(int i = 0; i < num1; i++)
        {
            for(int j = 0; j < num2; j++)
            {
                pair<int, int> p1 = human[i];
                pair<int, int> p2 = house[j];
                int dis = abs(p1.first - p2.first) + abs(p1.second - p2.second);
                add(i + 1, num1 + j + 1, dis, 1);
            }
        }
        printf("%d\n", mincostflow());
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/creatorx/article/details/78155414