poj-2195 最小费用最大流or二分图km算法

本文探讨了在网格地图上,如何计算最少花费使所有小人进入不同房子的算法问题。介绍了两种解决方法:最小费用最大流算法和KM算法,并提供了详细的代码实现。

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

Going Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26221 Accepted: 13140

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

建图:将每一个男人与每一个房子建立一条有向有权边,权值是两点的曼哈顿距离。

方法一:最小费用最大流,设置一个源点与每一个人相连权值是0,一个汇点与每一个房子相连权值也是0,所有边的流是1。

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
using namespace std;
#define N 100005
const int inf = 0x3f3f3f;
struct node {
	int x, y;
};
struct edge {
	int to;int s;int v;int f;
	int next;
};
int n, m;
int cnt = 0, cmt = 0, ans = 0;
node home[N], man[N];
edge a[N];
int head[N], dis[N], vis[N], pre[N];
void bul(int s, int t, int w) {
	a[ans].s = s;
	a[ans].to = t;
	a[ans].v = w;
	a[ans].f = 1;
	a[ans].next = head[s];
	head[s] = ans++;

	a[ans].s = t;
	a[ans].to = s;
	a[ans].v = -w;
	a[ans].f = 0;
	a[ans].next = head[t];
	head[t] = ans++;
}
bool spfa(int s, int t) {
	for (int i = 0; i <= t; i++)
		dis[i] = inf;
	memset(vis, 0, sizeof(vis));
	memset(pre, -1, sizeof(pre));
	queue<int>q;
	q.push(s);
	vis[s] = 1;
	dis[s] = 0;
	while (!q.empty()) {
		int temp = q.front();
		q.pop();
		vis[temp] = 0;
		for (int i = head[temp]; i != -1; i = a[i].next) {
			int ts = a[i].to;
			if (a[i].f&&dis[temp] + a[i].v < dis[ts]) {
				dis[ts] = dis[temp] + a[i].v;
				pre[ts] = i;
				if (!vis[ts]) {
					vis[ts] = 1;
					q.push(ts);
				}
			}
		}
	}
	if (dis[t] == inf)
		return false;
	return true;
}
int MY(int s, int t) {
	int temp;
	int sum_a=0, mx;
	while (spfa(s, t)) {
		mx = inf;
		temp = t;
		while (pre[temp] != -1) {
			mx = min(mx, a[pre[temp]].f);
			temp = a[pre[temp]].s;
		}
		temp = t;
		while (pre[temp] != -1) {
			a[pre[temp]].f -= mx;
			a[pre[temp] ^ 1].f += mx;
			temp = a[pre[temp]].s;
		}
		sum_a += dis[t];
	}
	return sum_a;
}
int main() {
	string strs;
	while(1){
		cnt = 0, cmt = 0, ans = 0;
		memset(head, -1, sizeof(head));
		scanf("%d%d", &n, &m);
		if (n == m & n == 0)
			break;
		for (int i = 0; i < n; i++) {
			cin >> strs;
			for (int j = 0; j < m; j++) {
				if (strs[j] == 'H') {
					home[cnt].x = i;
					home[cnt++].y = j;
				}
				else if (strs[j] == 'm') {
					man[cmt].x = i;
					man[cmt++].y = j;
				}
			}
		}
		for (int i = 0; i < cmt; i++) {
			for (int j = 0; j < cnt; j++) {
				bul(i + 1, cnt + j + 1, abs(home[i].x - man[j].x) + abs(home[i].y - man[j].y));
			}
		}
		for (int i = 1; i <= cnt; i++)
			bul(0, i, 0);
		for (int i = cnt + 1; i <= cnt + cmt; i++)
			bul(i, 2 * cnt + 1, 0);
		cout << MY(0, 2 * cnt + 1) << endl;
	}
	return 0;
}

方法二:km的模板,个人感觉用km解决更方便。

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
#define N 1005
#define inf 0x3f3f3f
struct node {
	int x, y;
};
node home[N], man[N];
int mp[N][N], match[N], slack[N];
bool vis_man[N], vis_home[N];
int ex_man[N], ex_home[N];
int cnt, cmt, ans;
int n, m;
bool dfs(int u) {
	vis_man[u] = true;
	for (int i = 1; i <= cnt; i++) {
		if (vis_home[i])continue;
		int gap = ex_home[i] + ex_man[u] - mp[u][i];
		if (gap==0) {
			vis_home[i] = true;
			if (match[i] == -1 || dfs(match[i])) {
				match[i] = u;
				return true;
			}
		}
		else {
			slack[i] = min(gap, slack[i]);
		}
	}
	return false;
}
int KM() {
	memset(match, -1, sizeof(match));
	memset(ex_home, 0, sizeof(ex_home));
	for (int i = 1; i <= cnt; i++) {
		ex_man[i] = mp[i][1];
		for (int j = 2; j <= cmt; j++)
			ex_man[i] = max(ex_man[i], mp[i][j]);
	}
	for (int i = 1; i <= cnt; i++) {
		memset(slack, inf, sizeof(slack));
		while (1) {
			memset(vis_man, false, sizeof vis_man);
			memset(vis_home, false, sizeof vis_home);
			if (dfs(i))break;
			int d = inf;
			for (int j = 1; j <= cmt; j++) {
				if (!vis_home[j])d = min(d, slack[j]);
			}
			for (int j = 1; j <= cnt; j++) {
				if (vis_man[j])ex_man[j] -= d;
				if (vis_home[j])ex_home[j] += d;
				else slack[j] -= d;
			}
		}
	}
	int res = 0;
	for (int i = 1; i <= cnt; i++) {
		res += mp[match[i]][i];
	}
	return -res;
}
int main() {
	string strs;
	while (1) {
		cnt = 0, cmt = 0, ans = 0;
		scanf("%d%d", &n, &m);
		if (n == m & n == 0)
			break;
		for (int i = 0; i < n; i++) {
			cin >> strs;
			for (int j = 0; j < m; j++) {
				if (strs[j] == 'H') {
					home[cnt].x = i;
					home[cnt++].y = j;
				}
				else if (strs[j] == 'm') {
					man[cmt].x = i;
					man[cmt++].y = j;
				}
			}
		}
		for (int i = 1; i <= cmt; i++) {
			for (int j = 1; j <= cnt; j++) {
				mp[i][j] = -(abs(man[i-1].x - home[j-1].x) + abs(man[i-1].y - home[j-1].y));
			}
		}
		cout << KM() << endl;
	}
	return 0;
}

顺便贴一个写km算法写的特别好的博客:http://www.cnblogs.com/wenruo/p/5264235.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值