Going Home(最小费用流)

92 篇文章 0 订阅
13 篇文章 1 订阅

Going HomeCrawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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
 
   
题目大意:n个人到m个房子分布在在坐标轴的不同位置,求解n个人到m间房子的最小花费(人可以上下左右移动,每移动一次就花费$1);
 
   
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <math.h>
using namespace std;
#define inf 999999999
struct node
{
    int x,y;
} house[300],man[300];// 分别保存房屋坐标和人的坐标
struct Edge
{
    int u, v, c, w;//c是容量;
    int next;
} edge[1000010];
int head[1010];
int dis[1010];
int father[1010];
int vis[1010];
char str[1010][1010];
int cnt;
void add(int u, int v, int c, int w)//加边;
{
    edge[cnt].u = u;//以下正向边;
    edge[cnt].v = v;
    edge[cnt].c = c;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
    edge[cnt].u = v;//以下反向边;
    edge[cnt].v = u;
    edge[cnt].c = 0;
    edge[cnt].w = -w;
    edge[cnt].next = head[v];
    head[v] = cnt++;
}
int SPFA(int s, int t, int n)
{
    int i, u, v;
    queue <int> q;
    memset(vis,false,sizeof(vis));
    memset(father,-1,sizeof(father));
    for(i=0; i<=n; i++)
        dis[i]=inf;
    vis[s] = 1;
    dis[s] = 0;
    q.push(s);//设0为超源点,入队
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        vis[u] = 0;//出队,队列中保存的是顶点不是边
        for(i = head[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].v;
            if(edge[i].c>0 && dis[v] > dis[u] + edge[i].w)//存在最短路且残余容量大于0可走
            {
                dis[v] = dis[u] + edge[i].w;
                father[v] = i;//保存路径,这里是保存关联的边edge[i]的下标
                if(!vis[v])//不在队列中
                {
                    q.push(v); //入队
                    vis[v] = 1;
                }
            }
        }
    }
    if(dis[t] == inf)
        return 0;//走不到超汇点,不存在增广路径
    return 1;
}
int cost(int s, int t, int n)
{
    int i;
    int minflow;
    int mincost=0;
    while(SPFA(s, t, n))//判断是否存在增广路径
    {
        minflow = inf;
        for(i = father[t]; i != -1; i = father[edge[i].u])
            if(edge[i].c < minflow)
                minflow = edge[i].c;//增广路径中求最小增量minflow
        for(i = father[t]; i != -1; i = father[edge[i].u])
        {
            edge[i].c-= minflow;//残存网络的残余容量
            edge[i^1].c += minflow;//反向边,edge[i].cost=-cost的作用与反向边同理
        }
        mincost += dis[t] * minflow;//加上这条增广路径的费用
    }
    return mincost;
}
int main()
{
    int n, m, i, j;
    int s,t;
    int nh,nm;
    while (scanf("%d %d", &n, &m))
    {
        if(n==0&&m==0)
            break;
        cnt=0;
        nh=0;
        nm=0;
        memset(head, -1, sizeof(head));
        for(i=0; i<n; i++)
        {
            scanf("%s",str[i]);
            for(j=0; j<m; j++)
            {
                if(str[i][j]=='m')//记录坐标
                {
                    man[nm].x=i;
                    man[nm++].y=j;
                }

                else if(str[i][j]=='H')
                {
                    house[nh].x=i;
                    house[nh++].y=j;
                }


            }
        }
        s = 0;//把人和房屋看做顶点从矩阵中抽象出来转化为网络
        t = nm+nh+1;
        for(i=0; i<nm; i++)
        {
            add(0,i+1,1,0);
            for(j=0; j<nh; j++)
            {
                int dist=fabs(house[i].x-man[j].x)+fabs(house[i].y-man[j].y);//人到房屋的距离
                add(i+1,j+1+nh,1,dist); //增加人到房屋的边的信息
            }
            add(i+1+nh,t,1,0); //增加房屋到超汇点的边信息
        }
        int ans = cost(s, t, t+1);
        printf("%d\n", ans);
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值