POJ 2195:Going Home

本文详细介绍了一个经典的费用流问题——送小人回家问题。通过构建费用流图的方式解决将多个小人分配到不同房屋中,每个小人需要找到一所未被占用的房子,并且要使得总的移动成本最小。文章提供了完整的代码实现及解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22869 Accepted: 11530

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

[Submit]   [Go Back]   [Status]   [Discuss]

题解:费用流裸题,建图可以这么考虑:建一个源点指向所有人,容量为1,代价为0,使每个人指向所有的房子,容量为1,代价为人与房子的曼哈顿距离,建一个汇点使所有房子指向它,容量为1,代价为0,然后最小费用最大流乱搞就OK了。POJ不能交#include<bits/stdc++.h>QAQ

贴上代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;  
#define MAXV 210  
#define INF 0x7fffffff  
  
typedef struct{  
    int x,y;  
}Point;  
  
Point man[MAXV],home[MAXV];  
int man_sum,home_sum,mincost;  
int cost[MAXV][MAXV],res[MAXV][MAXV],source,sink;  
int d[MAXV],parent[MAXV];  
  
void spfa(){  
    queue <int>q;  
    int v,i;  
    bool vis[MAXV];  
    memset(vis,false,sizeof(vis));  
    memset(parent,-1,sizeof(parent));  
    for(i=source;i<=sink;i++) d[i]=INF;  
  
    q.push(source);  
    d[source]=0;  
    vis[source]=true;  
  
    while(!q.empty()){  
        v=q.front();q.pop();  
        vis[v]=false;  
  
        for(i=source;i<=sink;i++){  
            if(res[v][i] && d[v]+cost[v][i]<d[i]){  
                d[i]=d[v]+cost[v][i];  
                parent[i]=v;  
                if(!vis[i]){  
                    q.push(i);  
                    vis[i]=true;  
                }  
            }  
        }  
    }  
}  
  
void MCMF(){  
    int v;  
    mincost=0;  
    while(1){  
          spfa();
        if(parent[sink]==-1) break; //到不了汇点 
  
        v=sink;  
        while(parent[v]!=-1){  
            res[parent[v]][v]-=1;  
            res[v][parent[v]]+=1;  
            v=parent[v];  
        }  
        mincost+=d[sink];  
    }  
}  
  
int main(){  
    int n,m,i,j;  
    char s[MAXV];  
    while(~scanf("%d%d",&n,&m) && n || m){  
        getchar();  
        man_sum=home_sum=0;  
        for(i=1;i<=n;i++){       //记录下人与房子的坐标  
            gets(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;  
                }  
            }  
        }  
  
        memset(cost,0,sizeof(cost));  
        memset(res,0,sizeof(res));  
        source=0;sink=home_sum+man_sum+1;  
  
        for(i=0;i<man_sum;i++) res[source][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);  
    }  
    return 0;  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值