going home hdu1533 二分匹配、完美匹配最小费用流

今天做了一道hdu1533的going home的题,题意是说找到平面上若干个人到若干个房子中的最小费用,同时要求全部都要有房子进入。易见可以容易地计算出其完全二分匹配的权重图,然后跑KM算法即可求得最大值(由于其为完全二分图,必然存在完美匹配)。而本题中要求最小值,可将边权设为负值来跑,最后获得结果再取负即可。

模板来自http://blog.csdn.net/x_y_q_/article/details/51927054。非常感谢!!!

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define MAXN 103
#define INF 0x3f3f3f3f

int ABS(int a) { return a >= 0 ? a : -a; }
typedef struct point {
	int x;
	int y;
	struct point(int i, int j) { x = i; y = j; }
}Point;
int price(Point a, Point b)
{
	return ABS(a.x - b.x) + ABS(a.y - b.y);
}
int visitX[MAXN], visitY[MAXN];
int leverX[MAXN], leverY[MAXN];
int yPair[MAXN];
char map[MAXN][MAXN];
int dist[MAXN][MAXN];
vector manPlaces;
vector housePlaces;
int N, M, mapSize;
void init()
{
	getchar();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			scanf("%c", &map[i][j]);
		}
		getchar();
	}
}
void printMap()
{
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			printf("%c%c", map[i][j], j == M - 1 ? '\n' : ' ');
		}
	}
}
void analyzeMap()
{
	manPlaces.clear();
	housePlaces.clear();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (map[i][j] == 'm')
				manPlaces.push_back(Point(i, j));
			else if (map[i][j] == 'H')
				housePlaces.push_back(Point(i, j));
		}
	}
	assert(manPlaces.size() == housePlaces.size());
	mapSize = manPlaces.size();
	for (int i = 0; i < mapSize; i++) {
		for (int j = 0; j < mapSize; j++) {
			dist[i][j] = -price(manPlaces[i], housePlaces[j]);
		}
	}
}
int can(int t)
{
	visitX[t] = 1;
	for (int i = 0; i < mapSize; i++) {
		if (!visitY[i] && leverX[t] + leverY[i] == dist[t][i]) {//这里“lx[t]+ly[i]==w[t][i]”决定了这是在相等子图中找增广路的前提,非常重要  
			visitY[i] = 1;
			if (yPair[i] == -1 || can(yPair[i])) {
				yPair[i] = t;
				return 1;
			}
		}
	}
	return 0;
}

int km()
{
	int sum = 0;
	memset(leverY, 0, sizeof(leverY));
	for (int i = 0; i < mapSize; i++) {//把各个lx的值都设为当前w[i][j]的最大值  
		leverX[i] = -INF;
		for (int j = 0; j < mapSize; j++) {
			if (leverX[i] < dist[i][j])
				leverX[i] = dist[i][j];
		}
	}
	memset(yPair, -1, sizeof(yPair ));
	for (int i = 0; i < mapSize; i++) {
		while (1) {
			memset(visitX, 0, sizeof(visitX ));
			memset(visitY, 0, sizeof(visitY ));
			if (can(i))//如果它能够形成一条增广路径,那么就break  
				break;
			int d = INF;//否则,后面应该加入新的边,这里应该先计算d值  
			for (int j = 0; j < mapSize; j++)//对于搜索过的路径上的XY点,设该路径上的X顶点集为S,Y顶点集为T,对所有在S中的点xi及不在T中的点yj  
				if (visitX[j])
					for (int k = 0; k < mapSize; k++)
						if (!visitY[k])
							d = min(d, leverX[j] + leverY[k] - dist[j][k]);
			if (d == INF)
				return -1;//找不到可以加入的边,返回失败(即找不到完美匹配)  
			for (int j = 0; j < mapSize; j++)
				if (visitX[j])
					leverX[j] -= d;
			for (int j = 0; j < mapSize; j++)
				if (visitY[j])
					leverY[j] += d;
		}
	}
	for (int i = 0; i < mapSize; i++)
		if (yPair[i] > -1)
			sum += dist[yPair[i]][i];
	return sum;
}
int main()
{
	while (scanf("%d %d", &N, &M) != EOF&&N&&M) {
		init();
		analyzeMap();
		printf("%d\n", -km());
	}
	return 0;
}

你可能感兴趣的:(oj,odyssey)