poj2195 Going Home [最小费用最大流]

in 题目 with 0 comment

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


Solution

读入图之后,遍历整张图,分别记录所有人和房子的位置,之后建图:

  1. 指定超级源s=0 , t=num_house+num_man+1
  2. 连接$s\rightarrow man[i]$ 与 $house[i]\rightarrow t$ 容量为1,费用为0,分别表示一个人只能进一个房间,一个房间只能进一次
  3. 每个人与每个房间连边,容量为1,费用为该人与该房间的曼哈顿距离

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#define maxn 250
#define maxe 50010
#define INF 0x3f3f3f3f
//#define DEBUG
using namespace std;
int n,m,s,t,cnt;
int num_man,num_house;
int head[maxn],dis[maxn],prev[maxn],path[maxn];
char mp[maxn][maxn];

struct pos
{
    int x,y;
    pos(int x=0,int y=0):x(x),y(y) {}
}man[maxn],house[maxn];

struct edge
{
    int to,cap,val,next;
    edge(int to=0,int cap=0,int val=0,int next=0):to(to),cap(cap),val(val),next(next) {}
}e[maxe];

inline void ins(int u,int v,int cap,int val)
{
    e[++cnt]=edge(v,cap,val,head[u]);
    head[u]=cnt;
}

inline void insert(int u,int v,int cap,int val)
{ ins(u,v,cap,val);ins(v,u,0,-val); }

void init()
{
    cnt=1;
    num_man=num_house=0;
    memset(head,0,sizeof(head));
}

void build_graph()
{
    for(int i=1;i<=n;++i) scanf("%s",mp[i]+1);
#ifdef DEBUG 
    cout<<endl<<"graph:"<<endl;
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j)
            cout<<mp[i][j];
        cout<<endl;}
    cout<<endl;
#endif
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        {
            if(mp[i][j]=='H')
                house[++num_house]=pos(i,j);
            if(mp[i][j]=='m')
                man[++num_man]=pos(i,j);
        }
#ifdef DEBUG
    cout<<"num_man="<<num_man<<" num_house="<<num_house<<endl;
    for(int i=1;i<=num_man;++i) cout<<"x="<<man[i].x<<" y="<<man[i].y<<endl;
    cout<<endl;
    for(int i=1;i<=num_house;++i) cout<<"x="<<house[i].x<<" y="<<house[i].y<<endl;
    cout<<endl;
#endif
    s=0,t=num_house+num_man+1;
    for(int i=1;i<=num_man;++i)
    {
        insert(s,i,1,0);
        for(int j=1;j<=num_house;++j)
        {
            int val=abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y);
        #ifdef DEBUG
            cout<<i<<"-->"<<j<<":"<<val<<endl; 
        #endif
            insert(i,j+num_man,1,val);
            if(i==1) insert(j+num_man,t,1,0);
        }
    }   
}

bool SPFA()
{
    memset(dis,0x3f,sizeof(dis));
    queue<int> q;
    q.push(s);
    dis[s]=0,prev[s]=-1;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        int i=head[u];
        while(i)
        {
            if(e[i].cap && dis[u]+e[i].val<dis[e[i].to])
            {
                dis[e[i].to]=dis[u]+e[i].val;
                prev[e[i].to]=u;
                path[e[i].to]=i;
                q.push(e[i].to); 
            }
            i=e[i].next;
        }
    }
    if(dis[t]==INF) return 0;
    return 1;
}

int argument()
{
    int w=INF,cost=0;
    for(int i=t;prev[i]!=-1;i=prev[i])
        w=min(w,e[path[i]].cap);
    for(int i=t;prev[i]!=-1;i=prev[i])
    {
        e[path[i]].cap-=w;
        e[path[i]^1].cap+=w;
        cost+=e[path[i]].val;
    }
    return cost;
}

void MCMF()
{
    int ans=0;
    while(SPFA()) ans+=argument();
    printf("%d\n",ans); 
}

int main()
{
    while(scanf("%d%d",&n,&m)==2 && n && m)
    {
        init();
        build_graph();
        MCMF();
    }
    return 0;
} 

扫描二维码,在手机上阅读!
Responses