【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;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值