前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图论--网络流--费用流POJ 2195 Going Home

图论--网络流--费用流POJ 2195 Going Home

作者头像
风骨散人Chiam
发布2020-10-28 11:53:47
5760
发布2020-10-28 11:53:47
举报
文章被收录于专栏:CSDN旧文

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

代码语言:javascript
复制
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

代码语言:javascript
复制
2
10
28

就是普通的费用流问题, 源点s编号0, 人编号1到n, 房子编号n+1到n+n, 汇点编号t,  源点s到每个人i有边(s, i, 1,0), 每个人i到每个房子j有边(i, j, 1, i人到j房的开销), 每个房子j到汇点t有边(j, t, 1, 0)。就成了最基本的费用流。

代码语言:javascript
复制
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#include<cmath>
#define INF 1e9
using namespace std;
const int maxn=200+5;
 
struct Edge
{
    int from,to,cap,flow,cost;
    Edge(){}
    Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
};
 
struct MCMF
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
 
    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;++i) G[i].clear();
    }
 
    void AddEdge(int from,int to,int cap,int cost)
    {
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
 
    bool BellmanFord(int &flow,int &cost)
    {
        for(int i=0;i<n;++i) d[i]=INF;
        queue<int> Q;
        memset(inq,0,sizeof(inq));
        d[s]=0, Q.push(s), a[s]=INF, p[s]=0, inq[s]=true;
        while(!Q.empty())
        {
            int u=Q.front(); Q.pop();
            inq[u]=false;
            for(int i=0;i<G[u].size();++i)
            {
                Edge &e=edges[G[u][i]];
                if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
                {
                    d[e.to]=d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]=min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){ Q.push(e.to); inq[e.to]=true; }
                }
            }
        }
        if(d[t]==INF) return false;
        flow +=a[t];
        cost +=a[t]*d[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].flow +=a[t];
            edges[p[u]^1].flow -=a[t];
            u=edges[p[u]].from;
        }
        return true;
    }
 
    int solve()
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        return cost;
    }
}MM;
 
struct Node
{
    int x,y;
    Node(){}
    Node(int x,int y):x(x),y(y){}
    int get_dist(Node& b)
    {
        return abs(x-b.x)+abs(y-b.y);
    }
}node1[maxn],node2[maxn];
 
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2 && n)
    {
        int num1=0,num2=0;//记录人数和房子数
        for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
        {
            char ch;
            scanf(" %c",&ch);
            if(ch=='m') node1[num1++]=Node(i,j);
            else if(ch=='H') node2[num2++]=Node(i,j);
        }
 
        int src=0,dst=num1*2+1;
        MM.init(num1*2+2,src,dst);
        for(int i=1;i<=num1;++i)
        {
            MM.AddEdge(src,i,1,0);
            MM.AddEdge(num1+i,dst,1,0);
            for(int j=1;j<=num1;++j)
            {
                MM.AddEdge(i,num1+j,1,node1[i-1].get_dist(node2[j-1]));
            }
        }
        printf("%d\n",MM.solve());
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/11/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档