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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 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 H;
    vector 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

 

你可能感兴趣的:(图论)