先上几个资料:

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

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. 装饰者模式及php实现

    装饰模式(Decorator Pattern) : 动态地给一个对象增加一些额外的职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更为灵活.其别名也可以称为包装器(Wr ...

  2. Kendo MVVM (二) ObservableObject 对象

    概述 Kendo MVVM 框架关键的一个部分为 ViewModel,它主要是通过 kendo.data.ObserableObject 来提供支持的.它可以监控改变( UI 变化或是值的变化)并通知 ...

  3. 一键部署基于SVN开源版本控制系统

    https://market.azure.cn/Vhd/Show?vhdId=11889&version=12961 产品详情 产品介绍Subversion作为新一代的开源版本控制工具,Sub ...

  4. String 对象详解

    原文地址:http://zangweiren.javaeye.com/blog/209895 作者:臧圩人(zangweiren) 网址:http://zangweiren.javaeye.com & ...

  5. HDU 4283 You Are the One (区间DP,经典)

    题意: 某校举行一场非诚勿扰,给定一个出场序列,表示n个人的屌丝值,如果他是第k个出场的,他的不满意度为(k-1)*diao[i].为了让所有人的屌丝值之和更小,导演设置一个栈,可以将部分人装进栈中, ...

  6. HDU 5489 Removed Interval (LIS,变形)

    题意: 给出一个n个元素的序列,要求从中删除任一段长度为L的连续子序列,问删除后的LIS是多少?(n<=10w, L<=n ,元素可能为负) 思路: 如果会O(nlogn)求普通LIS的算 ...

  7. hdparm - 获取/设置硬盘参数

    总览 hdparm [ -a [扇区数] ] [ -A [0|1] ] [ -c [芯片组模式] ] [ -C ] [ -d [0|1] ] [ -f ] [ -g ] [ -i ] [ -k [0| ...

  8. js 获取当前年月日时分秒星期

    $("#aa").click(function () { var date = new Date(); this.year = date.getFullYear(); this.m ...

  9. 如何远程连接Windows server上的MySQL服务

    废话不多说,直接开干 首先要打开服务器的MySQL端口号:3306(当然,也可以把服务器的防火墙直接关闭,不过不安全) 1.打开服务器管理器,有个高级安全Windows防火墙,下面有一个入站规则, 右 ...

  10. E​x​c​h​a​n​g​e​邮​箱​搭​建

    出现的问题: System.Runtime.InteropServices.COMException(0x8004020F): The server rejected one or more reci ...