先上几个资料:

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

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. ios中 input 焦点光标不垂直居中

    笔记:在ios,如果同时给input设置这种平时我们使字体垂直居中的css写法. 光标会出现,如下图的问题 . 改正方案: 采取不使用line-height的垂直居中方法即可.

  2. 在Unity3d中解析Lua脚本的方法

    由于近期项目中提出了热更新的需求,因此本周末在Lua的陪伴下度过.对Lua与Unity3d的搭配使用,仅仅达到了一个初窥门径的程度,记录一二于此.水平有限,欢迎批评指正. 网络上关于Lua脚本和Uni ...

  3. I/O————流

    流的关系图 缓冲流分为字节和字符缓冲流(图中是经常用的搭配,PrintWrite与BufferedWrite都继承java.io.Write) 字节缓冲流为: BufferedInputStream— ...

  4. hystrix 应用问题

    1.问题总结, 如果项目中使用了ThreadLocal,注意hystix创建新线程时,ThreadLocal中存的是之前线程中的数据,在hystix线程中获取不到 2.问题 throwable异常参数 ...

  5. Hello World另类写法

    #include<iostream> #define _ using namespace std; #define __ int main() #define ___ { #define ...

  6. VueJs $watch()方法总结!!

    最近公司用vue框架写交互,之前没怎么写过,但是很多数据双向绑定的东东跟angular很像!所以上手很快!哈哈 今天就碰到一个vue的问题啊!!产品需求是,datetimepick时间选择器一更改时间 ...

  7. 验证fgets末尾自动添加的字符是'\0' 还是 '\n\'?

    最近写代码经常使用字符串,对于输入函数fgets网上有人说输入结束会在末尾自动添加'\n',还有人说添加的是'\n',我决定亲自验证: #include "iostream" #i ...

  8. informix服务端卸载后重新安装不成功

    可能原因: 1.实例未删除 2.配置文件未删除 安装成功后远程客户端连接不上问题: 1..如果自己设置的数据库实例报错,换一个数据库实例(database)试试,例如sysadmin

  9. Linux Shell流程例子

    #!/bin/bash read -p "input a dight:"echo $REPLY DATE=`date`echo "DATE is ${DATE}" ...

  10. 2017-3-7-lint183-wood-cut

    2017-3-7-lint183-wood-cut 在河之洲 算法 lintcode problem lintcode183 wood cut solution 注意两点 注意边界条件 取的是最大值而 ...