POJ 2195 Going Home

A - Going Home
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
 
  
 
  
仅仅会了一点
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;

#define INF 0x7fffffff

struct node
{
    int x;///被标记的m或H的横坐标
    int y;///被标记的m或H的纵坐标
} a[400],b[400];
struct node1
{
    int x;///该边连向的点
    int y;///该边连向的点
    int z;///流量
    int w;///权值
    int next;///下一条边
} pp[600001];

int v[600001]; ///最短路的标记数组,
int pre[600001];///前驱数组,记录最短路经过的点
int num[600001];///最短路的距离数组,在这里称为花费数组,
int head[600001];///邻接表头数组 其初始化值为-1
int tt;///建边计数器   其初始值为0
int n,m;///矩阵的大小
int s,t;/// s超级源点   t超级汇点
char map[500][500];  ///地图

void add(int x,int y,int z,int w)///加边函数
{
    pp[tt].x = x;
    pp[tt].y = y;
    pp[tt].z = z;
    pp[tt].w = w;
    pp[tt].next = head[x];
    head[x] = tt++;
    pp[tt].x = y;
    pp[tt].y = x;
    pp[tt].z = 0;
    pp[tt].w = -w;
    pp[tt].next = head[y];
    head[y] = tt++;
}

int SPFA(int s,int t,int r)///基于邻接表的SPFA算法
{
    int i,u,vv;
    queue<int>q;
    for(i=0;i<=r;i++)
    {
        v[i] = 0;
        num[i] = INF;
        pre[i] = -1;
    }
    v[s] = 1;
    num[s] = 0;
    q.push(s);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        v[u] = 0;
        for(i=head[u];i!=-1;i=pp[i].next)
        {
            vv = pp[i].y;
            if(pp[i].z && num[vv] > num[u] + pp[i].w)
            {
                num[vv] = num[u] + pp[i].w;
                pre[vv] = i;
                if(!v[vv])
                {
                    q.push(vv);
                    v[vv] = 1;
                }
            }
        }
    }
    if(num[t] == INF)
    {
        return 0;///代表最增广短路寻找失败,while()停止
    }
    return 1;///代表已找到最短增广路
}

int KM(int s,int t,int r)
{
    int i,flag,minn = 0;
    while(SPFA(s,t,r))///返回1时while循环继续
    {
        flag = INF;
        for(i=pre[t];i!=-1;i = pre[pp[i].x])
        {
            flag = min(flag,pp[i].z);///寻找最小值
        }
        for(i=pre[t];i!=-1;i = pre[pp[i].x])
        {
            pp[i].z -= flag;///修改流量
            pp[i+1].z += flag;
        }
        //printf("num[%d] = %d\n",t,num[t]);
        //printf("flag = %d\n",flag);
        minn += num[t]*flag;
    }
    return minn;
}

int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(head,-1,sizeof(head));///初始化head数组的值
        if(n == 0 && m == 0)
        {
            break;
        }
        tt = 0;
        s = 0;
        t = n*m+1;
        int l = 0,u = 0;

        for(i=1; i<=n; i++)
        {
            scanf("%s",map[i]+1);
            for(j=1; j<=m; j++)
            {
                int x = (i-1)*m+j;///以点建图
                if(map[i][j] == 'm')
                {
                    add(s,x,1,0);///使m与超级源点相连
                    a[l].x = i;///记录下横坐标
                    a[l++].y = j;///记录下纵坐标
                }
                else if(map[i][j] == 'H')
                {
                    add(x,t,1,0);///使H与超级汇点相连
                    b[u].x = i;///记录下横坐标
                    b[u++].y = j;///记录下纵坐标
                }
            }
        }
        for(i=0; i<l; i++)
        {
            for(j=0; j<u; j++)
            {
                int h = abs(a[i].x - b[j].x)+abs(a[i].y - b[j].y);///找出第i个m分别到第j个H的步数
                int kk = (a[i].x-1)*m + a[i].y;///a[i]的标记的点数
                int ll = (b[j].x-1)*m + b[j].y;///b[j]的标记的点数
                add(kk,ll,1,h);///m与H建边
            }
        }
        printf("%d\n",KM(s,t,t+1));

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值