1. 程式人生 > >poj-2195 最小費用最大流or二分圖km演算法

poj-2195 最小費用最大流or二分圖km演算法

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