Going Home(网络流 最小费用最大流)

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22035 Accepted: 11139

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
//第一次网络流,恕我只能参考网上代码http://blog.csdn.net/wangjian8006/article/details/7940499

题目大意:图中有n个man和n个home,并且一个人只能住在一个房子里面,房子和人的个数是相等的。并且每个人移动一步的代价是1,怎么使 所有人住在房子里,并且使所有人的代价和最小。 给出一个n×m的图,m表示人,H表示房子,.表示空地,当然房子不算障碍物,可以穿过

解题思路:这是一个费用流的应用,构图如下:

建一个源点指向所有人,容量为1,代价为0 使每个人指向所有的房子,容量为1,代价为人与房子的曼哈顿距离 建一个汇点使所有房子指向它,容量为1,代价为0

然后从源点到汇点求费用流即可。

题目要注意的是,在n×m的这张图上说人的总数不超过100,那么图中节点总数为人+房子+源点+汇点=202

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
#define maxn 210
#define INF INT_MAX
struct point
{
    int x,y;
}man[maxn],home[maxn];
int man_sum,home_sum,mincost;
int cost[maxn][maxn],res[maxn][maxn],souce,sink;
int d[maxn],p[maxn];
void spfa()
{
    queue<int>q;
    int v,i;
    bool vis[maxn];
    memset(vis,0,sizeof(vis));
    memset(p,-1,sizeof(p));

    for(i=souce;i<=sink;i++)
        d[i]=INF;
    d[souce]=0;
    q.push(souce);
    vis[souce]=1;
    while(!q.empty())
    {
         v=q.front();
        q.pop();
        vis[v]=0;
        for( i=souce;i<=sink;i++)
        {
            if(res[v][i] && d[v]+cost[v][i]<d[i])
            {
                d[i]=d[v]+cost[v][i];
                p[i]=v;
                if(!vis[i])
                {
                    q.push(i);
                    vis[i]=1;
                }
            }
        }
    }
}

void MCMF()
{
    int v;
    mincost=0;
    while(1)
    {
        spfa();
        if(p[sink]==-1) break;
         v=sink;
        while(p[v]!=-1)
        {
            res[p[v]][v]-=1;
            res[v][p[v]]+=1;
            v=p[v];
        }
        //printf("---%d\n",d[sink]);
        mincost+=d[sink];
    }
}
int main()
{

    int n,m,i,j;
    char s[maxn];
    while(~scanf("%d%d",&n,&m),(n+m))
    {

        home_sum=0;man_sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            for(j=0;j<m;j++)
            {
                if(s[j] == 'm')
                {
                    man[man_sum].x=i;
                    man[man_sum++].y=j+1;
                }
                if(s[j]=='H')
                {
                    home[home_sum].x=i;
                    home[home_sum++].y=j+1;
                }
            }
        }
        //printf("%d  %d\n",man_sum,home_sum);
        memset(cost,0,sizeof(cost));
        memset(res,0,sizeof(res));
        souce=0;sink=home_sum+man_sum+1;
        for(i=0;i<man_sum;i++) res[souce][i+1]=1;//源点指向人  

        for(i=0;i<man_sum;i++)   //人指向房子  
        {
            for(j=0;j<home_sum;j++)
            {
                res[i+1][man_sum+j+1]=1;
                cost[i+1][man_sum+j+1]=abs(man[i].x-home[j].x)+abs(man[i].y-home[j].y);
                cost[man_sum+j+1][i+1]=-cost[i+1][man_sum+j+1];
            }
        }

        for(i=0;i<home_sum;i++)//房子指向汇点  
        {
            res[man_sum+i+1][sink]=1;
        }
        MCMF();
        printf("%d\n",mincost);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值