poj 3241 Object Clustering (曼哈顿距离最小生成树)

Object Clustering
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 1781 Accepted: 460

Description

We have (N ≤ 10000) objects, and wish to classify them into several groups by judgement of their resemblance. To simply the model, each object has 2 indexes a and b (ab ≤ 500). The resemblance of object i and object j is defined by dij = |a- aj| + |b- bj|, and then we say i is dij resemble to j. Now we want to find the minimum value of X, so that we can classify the N objects into K (< N) groups, and in each group, one object is at most X resemble to another object in the same group, i.e, for every object i, if i is not the only member of the group, then there exists one object j (i ≠ j) in the same group that satisfies dij ≤ X

Input

The first line contains two integers N and K. The following N lines each contain two integers a and b, which describe a object.

Output

A single line contains the minimum X.

Sample Input

6 2
1 2
2 3
2 2
3 4
4 3
3 1

Sample Output

2

曼哈顿距离最小生成树
详细讲解: http://blog.csdn.net/huzecong/article/details/8576908

/*======================================================
# Author: whai
# Last modified: 2015-10-09 21:07
# Filename: poj3241.cpp
======================================================*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>

using namespace std;

#define LL __int64
#define PB push_back
#define P pair<int, int>
#define X first
#define Y second

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;

struct Pt {
	int x, y, id;
}p[N];

bool cmp1(Pt a, Pt b) {
	if(a.x == b.x) return a.y < b.y;
	return a.x < b.x;
}

struct BIT {
	int minn, p;
	void init() {
		minn = INF;
		p = -1;
	}
}bit[N];

int lowbit(int x) { return x & -x; }

void update(int x, int v, int p) {
	while(x > 0) {
		if(v < bit[x].minn)
			bit[x].minn = v, bit[x].p = p;
		x -= lowbit(x);
	}
}

int query(int x, int limit) {
	int minn = INF, p = -1;
	while(x <= limit) {
		if(bit[x].minn < minn)
			minn = bit[x].minn, p = bit[x].p;
		x += lowbit(x);
	}
	return p;
}

struct E {
	int u, v, d;
}e[N * 2];
int e_tot;
void add_edge(int u, int v, int d) {
	e[e_tot++] = (E){u, v, d};
}

bool cmp2(E a, E b) {
	return a.d < b.d;
}

int fa[N];
int get_fa(int x) {
	if(x != fa[x]) return fa[x] = get_fa(fa[x]);
	return x;
}

int _abs(int x) {
	if(x < 0) return -x;
	return x;
}

int dis(Pt a, Pt b) {
	return _abs(a.x - b.x) + _abs(a.y - b.y);
}

int mmst(int n, int k) {
	int a[N], b[N];
	for(int dir = 0; dir < 4; ++dir) {
		if(dir == 1 || dir == 3) {
			for(int i = 0; i < n; ++i)
				swap(p[i].x, p[i].y);
		} else if(dir == 2) {
			for(int i = 0; i < n; ++i)
				p[i].x = -p[i].x;
		}
		sort(p, p + n, cmp1);
		for(int i = 0; i < n; ++i)
			a[i] = b[i] = p[i].y - p[i].x;
		sort(b, b + n);
		int m = unique(b, b + n) - b;
		for(int i = 1; i <= m; ++i)
			bit[i].init();
		for(int i = n - 1; i >= 0; --i) {
			int pos = lower_bound(b, b + m, a[i]) - b + 1;
			int ans = query(pos, m);
			if(ans != -1)
				add_edge(p[i].id, p[ans].id, dis(p[i], p[ans]));
			update(pos, p[i].x + p[i].y, i);
		}
	}
	sort(e, e + e_tot, cmp2);
	int cnt = n - k;
	for(int i = 0; i < n; ++i)
		fa[i] = i;
	for(int i = 0; i < e_tot; ++i) {
		int u = e[i].u, v = e[i].v;
		int fu = get_fa(u), fv = get_fa(v);
		if(fu != fv) {
			--cnt;
			fa[fv] = fu;
			if(cnt == 0)
				return e[i].d;
		}
	}
}

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; ++i) {
		scanf("%d%d", &p[i].x, &p[i].y);
		p[i].id = i;
	}
	printf("%d\n", mmst(n, k));
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值