POJ2195-Going Home

63 篇文章 0 订阅
16 篇文章 0 订阅

Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22253 Accepted: 11235

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


题意:给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致。man每移动一格需花费$1(即单位费用=单位距离),一间house只能入住一个man。现在要求所有的man都入住house,求最小费用。

 解题思路: 把man作为一个顶点集合U,house作为另一个顶点集合V,把U中所有点到V中所有点连线,费用cost[u][v]为abs(△x)+abs(△y),反向弧费用cost[v][u]= -cost[u][v],容量cap[u][v]=1,构成一个多源多汇的二分图。超级源s与U中所有点相连,费用cost[s][u]=0,容量cap[s][u]=1;V中所有点与超级汇t相连,费用cost[v][t]=0,容量cap[t][v]=1。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 60100  
#define MAXM 1000100  

int vis[MAXN],d[MAXN],pre[MAXN],a[MAXN];

struct Edge
{
	int u, v, c, cost, next;
} edge[MAXM];
int s[MAXN], cnt;

void init()
{
	cnt = 0;
	memset(s, -1, sizeof(s));
}

void add(int u, int v, int c, int cost)
{
	edge[cnt].u = u;
	edge[cnt].v = v;
	edge[cnt].cost = cost;
	edge[cnt].c = c;
	edge[cnt].next = s[u];
	s[u] = cnt++;
	edge[cnt].u = v;
	edge[cnt].v = u;
	edge[cnt].cost = -cost;
	edge[cnt].c = 0;
	edge[cnt].next = s[v];
	s[v] = cnt++;
}

bool spfa(int ss, int ee,int &flow,int &cost)
{
	queue<int> q;
	memset(d, INF, sizeof d);
	memset(vis, 0, sizeof vis);
	d[ss] = 0, vis[ss] = 1, pre[ss] = 0, a[ss] = INF;
	q.push(ss);
	while (!q.empty())
	{
		int u = q.front();q.pop();
		vis[u] = 0;
		for (int i = s[u]; ~i; i = edge[i].next)
		{
			int v = edge[i].v;
			if (edge[i].c>0&& d[v]>d[u] + edge[i].cost)
			{
				d[v] = d[u] + edge[i].cost;
				pre[v] = i;
				a[v] = min(a[u], edge[i].c);
				if (!vis[v])
				{
					vis[v] = 1;
					q.push(v);
				}
			}
		}
	}
	if (d[ee] == INF) return 0;
	flow += a[ee];
	cost += d[ee]*a[ee];
	int u = ee;
	while (u != ss)
	{
		edge[pre[u]].c -= a[ee];
		edge[pre[u] ^ 1].c += a[ee];
		u = edge[pre[u]].u;
	}
	return 1;
}

int MCMF(int ss, int ee)
{
	int cost = 0, flow=0;
	while (spfa(ss, ee, flow, cost));
	return cost;
}

struct node
{
	int x, y;
}xx[MAXN], yy[MAXN];

char mp[202][202];
int sum1, sum2;

int main()
{
	int n, m;
	while (~scanf("%d%d", &n, &m) && (n + m))
	{
		init();
		sum1 = 1; sum2 = 1;
		for (int i = 0; i < n; i++)
		{
			scanf("%s", mp[i]);
			for (int j = 0; j < m; j++)
			{
				if (mp[i][j] == 'H')
				{
					xx[sum1].x = i;
					xx[sum1++].y = j;
				}
				else if (mp[i][j] == 'm')
				{
					yy[sum2].x = i;
					yy[sum2++].y = j;
				}
			}
		}
		sum1--; sum2--;
		int s = 0, e = sum1 + sum2 + 1;
		for (int i = 1; i <= sum2; i++)
			add(s, i, 1, 0);
		for (int i = 1; i <= sum1; i++)
			add(sum2 + i, e, 1, 0);
		for (int i = 1; i <= sum2; i++)
		{
			for (int j = 1; j <= sum1; j++)
			{
				int k = abs(xx[j].x - yy[i].x) + abs(xx[j].y - yy[i].y);
				add(i, sum2 + j, 1, k);
			}
		}
		printf("%d\n", MCMF(s, e));
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值