【最小费用最大流】D - Going Home POJ - 2195

Going Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24977 Accepted: 12517

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

Pacific Northwest 2004

 

 

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <cmath>
#define P pair<int, int>
using namespace std;

const int inf = 0x7fffffff;
const int mn = 220;

struct edge
{
	int to, cap, cost, rev;
};
vector<edge> G[2 * mn];
void addedge(int u, int v, int w, int f)
{
	edge e;
	e.to = v, e.cap = w, e.cost = f, e.rev = (int)G[v].size();
	G[u].push_back(e);
	e.to = u, e.cap = 0, e.cost = -f, e.rev = (int)G[u].size() - 1;
	G[v].push_back(e);
}

int h[mn];
int dis[mn];
int preve[mn], prevv[mn];  /// 前驱结点及对应边
int res;
void min_cost_flow(int s, int t, int f)
{
	memset(h, 0, sizeof h);
	while (f > 0)  /// dijkstra 更新h
	{
		priority_queue<P, vector<P>, greater<P> > que;
		for (int i = 0; i <= mn; i++)
			dis[i] = inf;
		dis[s] = 0;
		que.push(P(0, s));
		while (!que.empty())
		{
			P p = que.top();
			que.pop();
			if (dis[p.second] < p.first)
				continue;
			int v = p.second;
			for (int i = 0; i <(int)G[v].size(); i++)
			{
				edge &e = G[v][i];
				if (e.cap > 0 && dis[e.to] > dis[v] + e.cost + h[v] - h[e.to])
				{
					dis[e.to] = dis[v] + e.cost + h[v] - h[e.to];
					prevv[e.to] = v;
					preve[e.to] = i;
					que.push(P(dis[e.to], e.to));
				}
			}
		}

		/// 无法增广 得到答案
		if (dis[t] == inf)
			break;
		for (int i = 1; i <= mn; i++)
			h[i] += dis[i];
		int d = f;
		for (int v = t; v != s; v = prevv[v])
			d = min(d, G[prevv[v]][preve[v]].cap);

		f -= d;
		res += d * h[t];
		for (int v = t; v != s; v = prevv[v])
		{
			edge &e = G[prevv[v]][preve[v]];
			e.cap -= d;
			G[v][e.rev].cap += d;
		}
	}
}

struct node
{
	int x, y;
} M[mn], H[mn];

int main()
{
	//freopen("D:\\in.txt", "r", stdin);

	int n, m;
	while (~scanf("%d %d", &n, &m) && (n || m))
	{
		res = 0;
		int V = 0;
		for (int i = 0; i <= 2 * mn; i++)
			G[i].clear();

		char ch[110][110];
		for (int i = 0; i < n; i++)
			scanf("%s", ch[i]);

		int hou = 0, man = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				if (ch[i][j] == 'H')
				{
					V++;
					H[++hou].x = i, H[hou].y = j;
				}
				else if (ch[i][j] == 'm')
					M[++man].x = i, M[man].y = j;
			}
		}

		for (int i = 1; i <= V; i++)  // 每个男孩向每所房子连边
		{
			for (int j = 1; j <= V; j++)
			{
				int a = M[i].x - H[j].x;
				if (a < 0)
					a = -a;
				int b = M[i].y - H[j].y;
				if (b < 0)
					b = -b;
                addedge(i, 100 + j, inf, a + b);
			}
		}

		int s = 0, t = 201;
		for (int i = 1; i <= V; i++)
		{
			addedge(s, i, 1, 0);
			addedge(i + 100, t, 1, 0);
		}
		min_cost_flow(s, t, V);

		printf("%d\n", res);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值