1. 程式人生 > >【POJ 2195】 Going Home(最小費)

【POJ 2195】 Going Home(最小費)

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20121 Accepted: 10192

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



最小費用最大流問題,以前一直以為這個跟最小費不是一個問題(當時不造最小費怎麼玩,現在想想還是太naive。。。)

題目中說最多100個房子 人和房子的數量又一樣,這樣加上一個超級源點跟超級匯點 總共是0 [1,maxman] [maxman,maxman+maxhouse] maxman+maxhouse+1

然後讓超級源點到每個人流量1 消費0 每個人到每個房子流量1 消費為距離 房子到匯點流量1 消費0

然後就不斷搜 一直搜到不能再增廣即可

程式碼如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>

using namespace std;
const int INF = 0x3f3f3f3f;

struct Point
{
    int x,y;
    Point(){}
    Point(int _x,int _y):x(_x),y(_y){}
    int operator ^ (const struct Point a)
    {
        return abs(x-a.x)+abs(y-a.y);
    }
};

struct Edge
{
    int v,cost,cup;
    int next;
    Edge(){}
    Edge(int _v,int _cost,int _cup,int _next):v(_v),cost(_cost),cup(_cup),next(_next){}
};


//鄰接表建圖
int head[233];
Edge eg[40808];
int tp;
//存人跟房子的座標
Point house[102],man[102];
int tph,tpm;
//搜最小費 更新流量
int dis[233],pre[233];
bool vis[233];
int n,m,st,en,cost;

void init()
{
    memset(head,-1,sizeof(head));
    tph = tpm = 0;
    tp = 0;
}

void Add(int u,int v,int cost,int cup)
{
    //printf("%d->%d %d %d\n",u,v,cost,cup);
    eg[tp] = Edge(v,cost,cup,head[u]);
    head[u] = tp++;
    eg[tp] = Edge(u,-cost,0,head[v]);
    head[v] = tp++;
}

bool spfa()
{
    int minflow = INF;
    memset(dis,INF,sizeof(dis));
    memset(vis,0,sizeof(vis));
    pre[en] = -1;
    queue <int> q;
    q.push(st);
    vis[st] = 1;
    pre[st] = -1;
    dis[st] = 0;

    while(!q.empty())
    {
        int u = q.front();
        vis[u] = 0;
        q.pop();
        for(int i = head[u]; i != -1; i = eg[i].next)
        {
            int v = eg[i].v;
            //printf("%d %d\n",u,v);
            if(dis[v] > dis[u]+eg[i].cost && eg[i].cup)
            {
                dis[v] = dis[u]+eg[i].cost;
                minflow = min(minflow,eg[i].cup);
                pre[v] = i;

                if(!vis[v])
                {
                    q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }

    if(pre[en] == -1) return 0;
    cost += dis[en];

    for(int i = pre[en]; i != -1;i = pre[eg[i^1].v])
    {
        eg[i].cup -= minflow;
        eg[i^1].cup += minflow;
    }
    return 1;
}

int main()
{
    char str[233];
    while(scanf("%d %d",&n,&m) && (n+m))
    {
        init();
        for(int i = 1; i <= n; ++i)
        {
            scanf("%s",str+1);
            for(int j = 1; j <= m; ++j)
            {
                if(str[j] == 'm')
                {
                    man[++tpm] = Point(i,j);
                }
                else if(str[j] == 'H')
                {
                    house[++tph] = Point(i,j);
                }
            }
        }


        st = 0, en = tpm*2+1;
        for(int i = 1; i <= tpm; ++i)
            Add(st,i,0,1);

        for(int i = tpm+1; i <= tpm*2; ++i)
            Add(i,en,0,1);

        for(int i = 1; i <= tpm; ++i)
            for(int j = 1; j <= tph; ++j)
                Add(i,tpm+j,man[i]^house[j],1);

        cost = 0;
        while(spfa());
        printf("%d\n",cost);
    }
    return 0;
}