POJ 3241 Object Clustering 曼哈顿距离最小生成树

题目大意:求出曼哈顿距离最小生成树上的第k大边权。

思路:首先,你要了解:http://blog.csdn.net/acm_cxlove/article/details/8890003

也就是说,我们以每一个点为中心,把平面分成8个部分,每一个部分我们只需要离这个点最近的点。然后加上建一条边连接这个边和最近的点。然后就是MST。

听说这个算法是莫队算法的基础,我现在就去学。

CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 10010
#define INF 0x3f3f3f3f
using namespace std;

struct Edge{
	int x,y,length;

	bool operator <(const Edge &a)const {
		return length < a.length;
	}
	Edge(int _,int __,int ___):x(_),y(__),length(___) {}
	Edge() {}
}edge[MAX << 3];
struct Point{
	int x,y,_id;

	bool operator <(const Point &a)const {
		if(x == a.x)	return y < a.y;
		return x < a.x;
	}
	void Read() {
		scanf("%d%d",&x,&y);
	}
}point[MAX];

int points,k,edges;
int fenwick[MAX];

pair<int,int *> xx[MAX];
int y_x[MAX];

int father[MAX];

inline int CalcM(const Point &a,const Point &b)
{
	return abs(a.x - b.x) + abs(a.y - b.y);
}

inline int GetPos(int x)
{
	int re = 0;
	for(; x <= points; x += x&-x)
		if(point[fenwick[x]].x + point[fenwick[x]].y < point[re].x + point[re].y)
			re = fenwick[x];
	return re;
}

inline void Fix(int x,int pos)
{
	for(; x; x -= x&-x)
		if(point[fenwick[x]].x + point[fenwick[x]].y > point[pos].x + point[pos].y)
			fenwick[x] = pos;
}

void MakeGraph()
{
	for(int dir = 1; dir <= 4; ++dir) {
		if(dir == 2 || dir == 4)
			for(int i = 1; i <= points; ++i)
				swap(point[i].x,point[i].y);
		if(dir == 3)
			for(int i = 1; i <= points; ++i)
				point[i].y *= -1;

		sort(point + 1,point + points + 1);
		memset(fenwick,0,sizeof(fenwick));
		for(int i = 1; i <= points; ++i)
			xx[i] = make_pair(point[i].y - point[i].x,&y_x[i]);
		sort(xx + 1,xx + points + 1);
		int t = 0;
		xx[0].first = INF;
		for(int i = 1; i <= points; ++i) {
			t += (xx[i].first != xx[i - 1].first);
			*xx[i].second = t;
		}
		for(int i = points; i; --i) {
			int temp = GetPos(y_x[i]);
			if(temp)
				edge[++edges] = Edge(point[i]._id,point[temp]._id,CalcM(point[i],point[temp]));
			Fix(y_x[i],i);
		}
	}
}

int Find(int x)
{
	if(father[x] == x)	return father[x];
	return father[x] = Find(father[x]);
}

int MST()
{
	sort(edge + 1,edge + edges + 1);
	for(int i = 1; i <= edges; ++i) {
		int fx = Find(edge[i].x);
		int fy = Find(edge[i].y);
		if(fx != fy) {
			father[fx] = fy;
			if(!--k)
				return edge[i].length;
		}
	}
	return 0;
}

int main()
{
	cin >> points >> k;
	k = points - k;
	point[0].x = point[0].y = INF;
	for(int i = 1; i <= points; ++i) {
		point[i].Read();
		point[i]._id = i;
		father[i] = i;
	}
	MakeGraph();
	cout << MST() << endl;
	return 0;
}

时间: 2024-12-11 12:17:17

POJ 3241 Object Clustering 曼哈顿距离最小生成树的相关文章

poj 3241 Object Clustering 曼哈顿最小生成树

题意: 平面上有n个点,现在把他们分成k个集合,使得每个集合中的每个点都至少有一个本集合的点之间的曼哈顿距离不大于X,求最小的X. 分析: 转化为求n个点生成完全图的最小生成树的第k大边.接下来有几个重点. 1)根据莫队算法,由于边权是点的曼哈顿距离,每个点只需要跟周围8个方向中每个方向最近的点连边,这样算出的图与用完全图算出的最小生成树一样,涉及的边却大大减少. 2)用树状数组维护y右偏45度的最近点,每个点以y-x的位置,y+x的值放入树状数组,由于每次是查询区间(pos,last)的情况,

POJ 3241 Object Clustering 二维平面曼哈顿距离最小生成树

题目链接:点击打开链接 题意: 给定二维平面上的n个点坐标,常数k 下面n行给出坐标 求一个最小生成树,问第k大的边是多少. 任意两个点间建一条边的花费是其曼哈顿距离. 思路:转自:点击打开链接 一.曼哈顿距离最小生成树 曼哈顿距离最小生成树问题可以简述如下: 给定二维平面上的N个点,在两点之间连边的代价为其曼哈顿距离,求使所有点连通的最小代价. 朴素的算法可以用O(N2)的Prim,或者处理出所有边做Kruskal,但在这里总边数有O(N2)条,所以Kruskal的复杂度变成了O(N2logN

平面点曼哈顿最小生成树——POJ 3241 Object Clustering

对应POJ题目:点击打开链接 Object Clustering Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 1697   Accepted: 418 Description We have N (N ≤ 10000) objects, and wish to classify them into several groups by judgement of their resemblance. To simply

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

Object Clustering Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 2640   Accepted: 806 Description We have N (N ≤ 10000) objects, and wish to classify them into several groups by judgement of their resemblance. To simply the model, each

POJ 3241 Object Clustering(Manhattan MST)

题目链接:http://poj.org/problem?id=3241 Description We have N (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 (a, b ≤ 500). The resemblance of ob

POJ 3241 Object Clustering 莫队算法

第n-k大曼哈顿距离,莫队算法裸题 Object Clustering Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 1584   Accepted: 366 Description We have N (N ≤ 10000) objects, and wish to classify them into several groups by judgement of their resemblance. To simp

POJ3241 Object Clustering 曼哈顿最小生成树

题意:转换一下就是求曼哈顿最小生成树的第n-k条边 参考:莫涛大神的论文<平面点曼哈顿最小生成树> /* Problem: 3241 User: 96655 Memory: 920K Time: 94MS Language: C++ Result: Accepted */ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cs

LA 3662 Another Minimum Spanning Tree (曼哈顿距离最小生成树 模板)

题目大意: 曼哈顿最小距离生成树 算法讨论: 同上. 这回的模板真的准了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 #include <cstdio> 6 using namespace std; 7 const int N = 100000 + 5; 8 const int M = N * 8; 9 type

二维平面曼哈顿距离最小生成树模版

#include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <vector> #include <string> #include <time.h> #i