poj 2195 Going Home (km匹配~~)

Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24515   Accepted: 12296

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. 
<center> </center>
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

                   就是让你分别给n个男人找到去n个房子的最短路线~~我们可以把男人和房子分别归为一类~把人和各个房子的最短距离当作边权~~这样的话就是让你找到一个边权越短越好的路径了~这样的话就可以看成最佳匹配用KM算法来做~~不过km是求最大的匹配边权~~所以我们直接将距离取反~~求出结果后输出其相反数就行了~。

#include <iostream>
#include <cstring>
#include <cstdio>
#include<algorithm>
using namespace std;
const int INF = 10000000;
struct ***
{
	int x, y;
}man[105], house[105];
int dis[105][105];
int ex_man[105];
int ex_house[105];
bool vis_man[105];
bool vis_house[105];
int match[105];
int slack[105];
int n, m;
int M_cnt, H_cnt;
void init()
{
	M_cnt = 0;
	H_cnt = 0;
}
int hh(int x)
{
	if (x < 0)
	{
		return -x;
	}
	return x;
}
bool dfs(int man)
{
	vis_man[man] = true;
	for (int house = 0; house < M_cnt; ++house)
	{

		if (vis_house[house]) continue;
		int gap = ex_man[man] + ex_house[house] - dis[man][house];
		if (gap == 0)
		{
			vis_house[house] = true;
			if (match[house] == -1 || dfs(match[house]))
			{
				match[house] = man;
				return true;
			}
		}
		else
		{
			slack[house] = min(slack[house], gap);
		}
	}
	return false;
}
int KM()
{
	memset(match, -1, sizeof match);
	memset(ex_house, 0, sizeof ex_house);
	for (int i = 0; i < M_cnt; ++i)
	{
		ex_man[i] = dis[i][0];
		for (int j = 1; j < M_cnt; ++j)
		{
			ex_man[i] = max(ex_man[i], dis[i][j]);
		}
	}
	for (int i = 0; i < M_cnt; ++i)
	{
		for (int s = 0; s < 101; s++)
		{
			slack[s] = INF;
		}
		while (1)
		{
			memset(vis_man, false, sizeof vis_man);
			memset(vis_house, false, sizeof vis_house);
			if (dfs(i))
			{
				break;
			}
			int d = INF;
			for (int j = 0; j < M_cnt; ++j)
			{
				if (!vis_house[j])
				{
					d = min(d, slack[j]);
				}
			}
			for (int j = 0; j < M_cnt; ++j)
			{
				if (vis_man[j]) ex_man[j] -= d;
				if (vis_house[j]) ex_house[j] += d;
				else slack[j] -= d;
			}
		}
	}
	int res = 0;
	for (int i = 0; i < M_cnt; ++i)
		res += dis[match[i]][i];
	return res;
}
int main()
{
	while (~scanf("%d%d", &n, &m) && n + m)
	{
		init();
		for (int s = 1; s <= n; s++)
		{
			for (int e = 1; e <= m; e++)
			{
				char w;
				cin >> w;
				if (w == 'm')
				{
					man[M_cnt].x = s;
					man[M_cnt++].y = e;
				}
				else if (w == 'H')
				{
					house[H_cnt].x = s;
					house[H_cnt++].y = e;
				}
			}
		}
		for (int s = 0; s < M_cnt; s++)
		{
			for (int e = 0; e < H_cnt; e++)
			{
				dis[s][e] = -(hh(man[s].x - house[e].x) + hh(man[s].y - house[e].y));
			}
		}
		cout << -KM() << endl;
	}
	return 0;
}

全部评论

相关推荐

头像
11-18 16:08
福州大学 Java
影流之主:干10年不被裁,我就能拿别人一年的钱了,日子有盼头了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 10:52
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务