码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 1411 - Ants(二分图完美匹配)

时间:2014-09-04 13:27:19      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   ar   for   代码   sp   

UVA 1411 - Ants

题目链接

题意:给定一些黑点白点,要求一个黑点连接一个白点,并且所有线段都不相交

思路:二分图完美匹配,权值存负的欧几里得距离,这样的话,相交肯定比不相交权值小,所以做一次完美匹配就可以了

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int MAXNODE = 105;

typedef double Type;
const Type INF = 0x3f3f3f3f;

struct KM {
	int n;
	Type g[MAXNODE][MAXNODE];
	Type Lx[MAXNODE], Ly[MAXNODE], slack[MAXNODE];
	int left[MAXNODE];
	bool S[MAXNODE], T[MAXNODE];

	void init(int n) {
		this->n = n;
	}

	void add_Edge(int u, int v, Type val) {
		g[u][v] = val;
	}

	bool dfs(int i) {
		S[i] = true;
		for (int j = 0; j < n; j++) {
			if (T[j]) continue;
			double tmp = Lx[i] + Ly[j] - g[i][j];
			if (fabs(tmp) < 1e-9) {
				T[j] = true;
				if (left[j] == -1 || dfs(left[j])) {
					left[j] = i;
					return true;
				}
			} else slack[j] = min(slack[j], tmp);
		}
		return false;
	}

	void update() {
		double a = INF;
		for (int i = 0; i < n; i++)
			if (!T[i]) a = min(a, slack[i]);
		for (int i = 0; i < n; i++) {
			if (S[i]) Lx[i] -= a;
			if (T[i]) Ly[i] += a;
		}
	}

	void km() {
		for (int i = 0; i < n; i++) {
			left[i] = -1;
			Lx[i] = -INF; Ly[i] = 0;
			for (int j = 0; j < n; j++)
				Lx[i] = max(Lx[i], g[i][j]);
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) slack[j] = INF;
			while (1) {
				for (int j = 0; j < n; j++) S[j] = T[j] = false;
				if (dfs(i)) break;
				else update();
			}
		}
	}
} gao;

const int N = 105;
int n;
struct Point {
	double x, y;
	void read() {
		scanf("%lf%lf", &x, &y);
	}
} a[N], b[N];

double dis(Point a, Point b) {
	double dx = a.x - b.x;
	double dy = a.y - b.y;
	return sqrt(dx * dx + dy * dy);
}

int main() {
	int bo = 0;
	while (~scanf("%d", &n)) {
		if (bo) printf("\n");
		else bo = 1;
		gao.init(n);
		for (int i = 0; i < n; i++)
			a[i].read();
		for (int i = 0; i < n; i++)
			b[i].read();
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				gao.add_Edge(i, j, -dis(b[i], a[j]));
		gao.km();
		for (int i = 0; i < n; i++)
			printf("%d\n", gao.left[i] + 1);
	}
	return 0;
}


UVA 1411 - Ants(二分图完美匹配)

标签:style   http   color   os   io   ar   for   代码   sp   

原文地址:http://blog.csdn.net/accelerator_/article/details/39050519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!