POJ 2195 Going Home

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18013 Accepted: 9181

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
 
刚学费用流,理解的还不是很深刻,对于这个题,找到m的点,与源点S相连,找到H的点,与汇点T相连,在依次找到各点之间的距离,进行建图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#define Maxm 100001
#define inf 9999999
using namespace std;
struct node
{
	int v,u;
	int f;
	int w;
    int next;
}edge[Maxm];
 int n,m,S,T,cnt;
int head[Maxm];
int pre[Maxm],dis[Maxm],vis[Maxm];
void add(int x,int y,int f,int w)
{
    edge[cnt].u=x;
    edge[cnt].v=y;
    edge[cnt].f=f;
    edge[cnt].w=w;
    edge[cnt].next=head[x];
    head[x]=cnt++;

    edge[cnt].u=y;
    edge[cnt].v=x;
    edge[cnt].f=0;
    edge[cnt].w=-w;
    edge[cnt].next=head[y];
    head[y]=cnt++;

}
int spfa()
{
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    vis[S]=1;
    dis[S]=0;
    queue<int>q;
    q.push(S);
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        vis[t]=0;
        int r=head[t];
        while(r!=-1)
        {
            if(edge[r].f>0 && dis[edge[r].v] > dis[t] + edge[r].w)
            {
                dis[edge[r].v] = dis[t] + edge[r].w;
                 pre[edge[r].v]=r;
                if(!vis[edge[r].v])
                {
                    vis[edge[r].v]=1;
                    q.push(edge[r].v);

                }
            }
            r=edge[r].next;
        }
    }
    if(pre[T]==-1)
        return 0;
    else return 1;

}
int minfei()
{
    int ans=0;
    while(spfa())
    {
       int max1=inf;

       for(int t=pre[T];t!=-1;t=pre[edge[t].u])
       {
           max1=min(max1,edge[t].f);
       }
         for(int t=pre[T];t!=-1;t=pre[edge[t].u])
        {
            edge[t].f-=max1;
            edge[t+1].f+=max1;
            ans+= max1*edge[t].w;
        }
    }
    return ans;
}

int main()
{
     while(~scanf("%d%d",&n,&m)&&(m||n))
     {
         char map[110][110];
         int i,j;
         int x1[Maxm],y1[Maxm];
         int x2[Maxm],y2[Maxm];
         int s1=0,s2=0;
          S=0;
          T=m*n+1;
          cnt=0;
         memset(head,-1,sizeof(head));
         for(i=0;i<n;i++)
             scanf("%s",map[i]);
         for(i=0;i<n;i++)
         {
            for(j=0;j<m;j++)
             {
                 int t=i*m+j+1;
                 if(map[i][j]=='m')
                 {
                     add(S,t,1,0);
                     x1[s1]=i+1;
                     y1[s1++]=j+1;
                 }
                   if(map[i][j]=='H')
                 {
                     add(t,T,1,0);
                     x2[s2]=i+1;
                     y2[s2++]=j+1;
                 }
             }
         }

         for(i=0;i<s1;i++)
         {
             for(j=0;j<s2;j++)
             {
                 int s=abs(x1[i]-x2[j]) + abs(y1[i]-y2[j]) ;
                 int a=x1[i]*m+y1[i]-m;
                 int b=x2[j]*m+y2[j]-m;
                 add(a,b,1,s);
             }
         }

         printf("%d\n",minfei());

     }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值