POJ 2195:Going Home(SPFA最小费用最大流)

B - Going Home
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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个人n栋房子(H表示房子m表示人)现要给每个人分配一个人房子,请问如何分配才能

使所有人到家行走距离总和最短


最小费用最大流:每个边不但有个流量,还有一个单位费用,请问如何才能在最大流的前提下费用最小

增流网络:边(u,v)存的是当前路的单位费用,对应反向边(v,u)存的是当前路单位费用的负值

这个问题的算法和EK非常像!它是在EK的基础上改进而来的,算法核心:将边的费用转为边的长度,在寻找从起点

到重点的增广路时满足这条增广路为当前最短路(用SPFA,这个图中一定存在负边,但一定不可能有负环)


建图:初始化源点到所有人一个流量为1的边,所有房子到汇点一个流量为1的边,然后建人到房子的边,这些边流

为1,费用为人与房子的距离


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct
{
	int flow;
	int cost;
}Edge;
typedef struct
{
	int x;
	int y;
}Point;
Point h[105], p[105];
Edge road[505][505];
char str[105][105];
int dis, sink, link[505], vis[505], best[505];
int Spfa();
int main(void)
{
	int n, m, i, j, x, a, b, pre, ans;
	while(scanf("%d%d", &n, &m), n!=0 || m!=0)
	{
		a = b = 0;
		memset(road, 0, sizeof(road));
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf(" %c", &str[i][j]);
				if(str[i][j]=='H')
					h[++b].x = i, h[b].y = j;
				if(str[i][j]=='m')
					p[++a].x = i, p[a].y = j;
			}
		}
		n = a, m = b;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				dis = abs(h[j].x-p[i].x)+abs(h[j].y-p[i].y);
				road[i][n+j].flow = 1, road[i][n+j].cost = dis, road[n+j][i].cost = -dis;
			}
		}
		sink = n+m+1;
		for(i=1;i<=n;i++)
			road[0][i].flow = 1;
		for(i=1;i<=m;i++)
			road[n+i][sink].flow = 1;
		ans = 0;
		while(Spfa())
		{
			x = sink;
			dis = 10000000;
			while(x!=0)
			{
				pre = link[x];
				dis = min(dis, road[pre][x].flow);
				x = pre;
			}
			x = sink;
			while(x!=0)
			{
				pre = link[x];
				road[pre][x].flow -= dis;
				road[x][pre].flow += dis;
				ans += road[pre][x].cost*dis;
				x = pre;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

int Spfa()
{
	int i, now;
	memset(link, -1, sizeof(link));
	memset(vis, 0, sizeof(vis));
	memset(best, 2, sizeof(best));
	best[0] = 0;
	queue<int> q;
	q.push(0);
	while(q.empty()==0)		/*和EK不同的是,这里不但要找到一条增广路,而且这条增广路要是当前最短路!*/
	{
		now = q.front();
		q.pop();
		for(i=1;i<=sink;i++)
		{
			if(road[now][i].flow>0 && best[i]>best[now]+road[now][i].cost)
			{
				best[i] = best[now]+road[now][i].cost;
				link[i] = now;
				if(vis[i]==0)
				{
					vis[i] = 1;
					q.push(i);
				}
			}
		}
		vis[now] = 0;
	}
	if(best[sink]<=1000000)
		return 1;
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值