poj 2195 D - Going Home

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

题目大意:有相同个数的房子和人,每个人进入一个房子,每个房子只能装一个人,人进入房间的费用为该人到房间的距离,问这么多人进入到房间的花费最小是多少?

分析:由于是最小花费,所以想到了最小费用流。

建图:创建源点到房子的边,边的容量为1,花费为0,因为源点是我们人为创建的,不需要花费。创建终点到人的边,边的容量为1,花费为0,原因同上。然后在这个地图中找到所有的m和H,接着用一条边去连接m和H。最终跑个最小费用流就行了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
const int MAXM=1e6+10;
const int INF=0x3f3f3f3f;
int n,m,N;
struct Edge{
    int to,next,cap,flow,cost,from;
}edge[MAXM];
int head[MAXM],cnt;
int pre[MAXM],dis[MAXM];
bool vis[MAXM];
void init()
{
    cnt=0;
    N=205;
    memset(head,-1,sizeof(head));
}
void addedge(int from,int to,int cap,int cost)
{
    edge[cnt].to=to;
    edge[cnt].from=from;
    edge[cnt].flow=0;
    edge[cnt].next=head[from];
    edge[cnt].cap=1;
    edge[cnt].cost=cost;
    head[from]=cnt++;

    edge[cnt].to=from;
    edge[cnt].from=to;
    edge[cnt].flow=0;
    edge[cnt].next=head[to];
    edge[cnt].cap=0;
    edge[cnt].cost=-cost;
    head[to]=cnt++;

}
bool spfa(int s,int t)     ///沿着最短路增广
{
    queue<int> q;
    for(int i=0;i<=N;i++)
    {
        dis[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    dis[s]=0;
    vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1) return false;
    return true;
}
int minCostMaxflow(int s,int t)
{
    int flow=0;
    int cost =0;
    while(spfa(s,t))
    {
        int Min=INF;
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
        {
            if(Min>edge[i].cap-edge[i].flow)
                Min=edge[i].cap-edge[i].flow;
        }
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
        {
            edge[i].flow+=Min;
            edge[i^1].flow-=Min;
            cost+=edge[i].cost*Min;
        }
//        if(Min==0) break;
    }
    return cost;
}
struct pos
{
    int x;
    int y;
    pos(int a,int b)
    {
        x=a;
        y=b;
    }
};

void CIN()
{
    init();
    vector<pos> H;
    vector<pos> M_1;
    H.push_back(pos(0,0));
    M_1.push_back(pos(0,0));
    char str;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            scanf(" %c",&str);
            if(str=='H')
                H.push_back(pos(i,j));
            else if(str=='m')
                M_1.push_back(pos(i,j));
        }
    }
    int len1=H.size();
    int len2=M_1.size();
    for(int i=1; i<len1; i++)
    {
        for(int j=1; j<len2; j++)
        {
            int dis=abs(H[i].x-M_1[j].x)+abs(H[i].y-M_1[j].y);     ///加边
            addedge(i,len1+j,1,dis);
        }
    }
    for(int i=1; i<len1; i++)
        addedge(0,i,1,0);                                       ///在源点与房子之间加边
    for(int i=1; i<len2; i++)
        addedge(i+len1,205,1,0);                               ///在人与终点之间加边
    H.clear();
    M_1.clear();
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        CIN();
        printf("%d\n",minCostMaxflow(0,205));       ///我设置的源点节点编号为0,终点节点编号为205
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值