先上几个资料:

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

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. marquee标签(跑马灯)

  2. EasyUI:Easyui parser的用法

    Easyui的渲染机制是个比较坑的事情,在项目开发中,遇到需要等其渲染完成后处理一些事情,比如为联动的下拉框选中默认值,为某些表单元素自动填充值等!这就需要用到Easyui parser解析器了.官方 ...

  3. unbuntu&vim&Kali的各种小知识

    1. vmware workstation 15.0.0 2.ubuntu-18.10-desktop  使用网络地址转换 VMware workstation 1.ctrl+alt 返回  unbu ...

  4. zend studio failed to create java virtual machine无法启动的解法

    zend studio failed to create java virtual machine 解决方案:在安装目录下修改ZendStudio.ini中第十四行处改成 -Xmx512M. -sta ...

  5. 用 label 控制 Pod 的位置

    默认配置下,Scheduler 会将 Pod 调度到所有可用的 Node.不过有些情况我们希望将 Pod 部署到指定的 Node,比如将有大量磁盘 I/O 的 Pod 部署到配置了 SSD 的 Nod ...

  6. Python 继承实现的原理(继承顺序)

    继承顺序 Python3 : 新式类的查找顺序:广度优先 新式类的继承: class A(object): Python2 3 都是了 MRO算法--生成一个列表保存继承顺序表 不找到底部 Pytho ...

  7. Java替换手机号掩码

    String tel = "18304072984"; // 括号表示组,被替换的部分$n表示第n组的内容 tel = tel.replaceAll("(\\d{3})\ ...

  8. github:Commit failed - exit code 1 received

    问题 使用github desktop 将项目提交到github,但提示Commit failed - exit code 1 received 开始以为名称过程,把名称改短,但还是失败. 原因 因为 ...

  9. GCD之dispatch queue

    GCD之dispatch queue iOS中多线程编程工具主要有: NSThread NSOperation GCD 这三种方法都简单易用,各有千秋.但无疑GCD是最有诱惑力的,因为其本身是appl ...

  10. SVN:The working copy is locked due to a previous error (一)

    使用 Cornerstone  时,碰到如题问题,SVN无法Update.Commit等操作. 解决办法:Working Copies ⟹ '右键' ⟹ Clean 即可解决! 尊重作者劳动成果,转载 ...