先上几个资料:

百度文库有详细的分析和证明

cxlove的博客

TopCoder Algorithm Tutorials

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; int n, m, k; struct Point
{
int x, y, id;
bool operator < (const Point& p) const {
return x < p.x || (x == p.x && y < p.y);
}
}; Point p[maxn]; int dist(Point a, Point b) { return abs(a.x-b.x) + abs(a.y-b.y); } struct Node
{
int min_val, pos;
void init() { min_val = INF; pos = -; }
}; inline int lowbit(int x) { return x & (-x); } Node BIT[maxn]; struct Edge
{
int u, v, d;
Edge(int u, int v, int d):u(u), v(v), d(d) {}
bool operator < (const Edge& e) const { return d < e.d; }
}; vector<Edge> edges; int pa[maxn];
int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); } int a[maxn], b[maxn]; void update(int x, int val, int pos) {
while(x) {
if(val < BIT[x].min_val) {
BIT[x].min_val = val;
BIT[x].pos = pos;
}
x -= lowbit(x);
}
} int query(int x) {
int val = INF, pos = -;
while(x <= m) {
if(BIT[x].min_val < val) {
val = BIT[x].min_val;
pos = BIT[x].pos;
}
x += lowbit(x);
}
return pos;
} int main()
{
//freopen("in.txt", "r", stdin); while(scanf("%d%d", &n, &k) == && n)
{
edges.clear();
for(int i = ; i < n; i++) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].id = i;
} //Select edges
for(int dir = ; dir < ; dir++) {
//Coordinate Transformation
if(dir == || dir == ) for(int i = ; i < n; i++) swap(p[i].x, p[i].y);
else if(dir == ) for(int i = ; i < n; i++) p[i].x = -p[i].x; sort(p, p + n);
for(int i = ; i < n; i++) a[i] = b[i] = p[i].y - p[i].x;
sort(b, b + n);
m = unique(b, b + n) - b;
for(int i = ; i <= m; i++) BIT[i].init(); for(int i = n - ; i >= ; i--) {
int pos = lower_bound(b, b + m, a[i]) - b + ;
int q = query(pos);
if(q != -) edges.push_back(Edge(p[i].id, p[q].id, dist(p[i], p[q])));
update(pos, p[i].x + p[i].y, i);
}
} //Kruskal
sort(edges.begin(), edges.end());
int cnt = n - k, ans;
for(int i = ; i < n; i++) pa[i] = i;
for(int i = ; i < edges.size(); i++) {
int u = edges[i].u, v = edges[i].v;
int pu = findset(u), pv = findset(v);
if(pu != pv) {
if(--cnt == ) { ans = edges[i].d; break; }
pa[pu] = pv;
}
} printf("%d\n", ans);
} return ;
}

代码君

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

  1. 【POJ 3241】Object Clustering 曼哈顿距离最小生成树

    http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...

  2. 51nod 1213 二维曼哈顿距离最小生成树

    1213 二维曼哈顿距离最小生成树 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间 ...

  3. 曼哈顿距离最小生成树 codechef Dragonstone

    曼哈顿距离最小生成树 codechef Dragonstone 首先,对于每一个点来说有用的边只有它向它通过 x=0,y=0,y=x,y=-x 切出来的八个平面的最近点. 证明 我不会 反正当结论记住 ...

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

    Object Clustering Description We have N (N ≤ 10000) objects, and wish to classify them into several ...

  5. [51nod1213]二维曼哈顿距离最小生成树

    二维平面上有N个坐标为整数的点,点x1 y1同点x2 y2之间的距离为:横纵坐标的差的绝对值之和,即:Abs(x1 - x2) + Abs(y1 - y2)(也称曼哈顿距离).求这N个点所组成的完全图 ...

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

    题目大意: 曼哈顿最小距离生成树 算法讨论: 同上. 这回的模板真的准了. #include <iostream> #include <cstring> #include &l ...

  7. 【Poj3241】Object Clustering

    Position: http://poj.org/problem?id=3241 List Poj3241 Object Clustering List Description Knowledge S ...

  8. 曼哈顿距离MST

    https://www.cnblogs.com/xzxl/p/7237246.html 讲的不错 /* 曼哈顿距离最小生成树 poj 3241 Object Clustering 按照上面的假设我们先 ...

  9. 【poj3241】 Object Clustering

    http://poj.org/problem?id=3241 (题目链接) MD被坑了,看到博客里面说莫队要写曼哈顿最小生成树,我就写了一个下午..结果根本没什么关系.不过还是把博客写了吧. 转自:h ...

随机推荐

  1. Java面向对象(构造方法、this、super)

    面向对象 今日内容介绍 u 构造方法 u this u super 第1章 构造方法 我们对封装已经有了基本的了解,接下来我们来看一个新的问题,依然以Person为例,由于Person中的属性都被pr ...

  2. Kendo MVVM 数据绑定(一) attr

    Kendo MVVM 数据绑定(一) attr Kendo UI MVVM 数据绑定支持的绑定属性有 attr, checked, click, custom , disabled,enabled, ...

  3. Android自定义组件系列【17】——教你如何高仿微信录音Toast

    一.Toast介绍 平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下 A toast is a view containing a quick little message ...

  4. C# List的使用

    1.所需引入的命名空间: using System.Collections.Generic; 2.初始化 [1]空: List<int> list = new List<int> ...

  5. Jenkins中启动从节点时,出现问题如何解决,问题:No Known Hosts...

    Jenkins中,启动从节点时,出现如下问题如何解决:/root/.ssh/known_hosts [SSH] No Known Hosts file was found at /root/.ssh/ ...

  6. wpf ListBox删除选择项(支持多项)

    搞了个ListBox删除选择项,开始老是不能把选择项删除干净,剩下几个.后来调试一下原来是ListBox在删除一个选择项之后立即更新,选择项也有变化.结果我想了个这样的方法来删除呵呵. Departm ...

  7. HDU 2147 kiki's game kiki的游戏(博弈,找规律)

    题意: 给一个有n*m 个格子的棋盘,将一个硬币放在右上角一格,每次可以往左/下/左下移动一格,碰到不能移动的局面者输. 思路: 找P/N状态.先将(n,1)归为P状态,那么能一步到达此位置的有3个位 ...

  8. [Java] 新手快速就业需要掌握的知识点

    目的:主要是分享下日常工作中使用到的技术点,根据二八定律快速掌握使用知识点,先就业再沉淀去积累经验.(个人建议仅供参考) 背景:目前一般来说,都是前后端分离.你只需要提供接口给前端,他来处理就可以了, ...

  9. 变量和数据类型&运算符

    变量和数据类型&运算符 变量 变量的作用:用来存储数据 变量命名的规范:字(字符串)下(_下划线)美($)人(¥) 数 (可以包括数字)骆驼 有意义(可以以字母,下划线,美元符号,人民币符号开 ...

  10. SC || 那些CheckStyle中的错误们

    lab5里给了我们一个checkstyle查代码风格的方法.. 然后 lab4代码 copy一份! 添加checkstyle! 项目 右键 checkstyle!(自信脸) 3s后——7256 war ...