思路:

已知:

要生成曼哈顿距离最小生成树,一个点最多和四周8个点连线,那8个点分别是将那个点四周360度平分成8个区间,每个区间里面和那个点曼哈顿距离最小的点,所以如果有n个点,那么最多有4n条边,然后就可以用kruskal算法去做。

 #include <iostream>    //poj3241 曼哈顿距离最小生成树第k大的边
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#define mid ((l+r)>>1)
using namespace std; int ent; int temp1,temp2,temp3; int fa[]; class Point
{
public:
int x,y,id;
} point[]; class Tree
{
public:
int index,current;
} tree[<<]; class Edge
{
public:
int s,t,dis;
} edge[]; bool cmp(Point a,Point b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
} bool cmp2(Edge a,Edge b)
{
return a.dis<b.dis;
} int get_ans(Point a)
{
return a.y-a.x+;
} int get_dis(int a,int b)
{
return point[b].x+point[b].y-point[a].x-point[a].y;
} void build(int l,int r,int n)
{
tree[n].index=INT_MAX;
if(l==r)
{
tree[n].current=l;
return;
}
build(l,mid,n<<);
build(mid+,r,n<<|);
} void update(int l,int r,int n,int current,int aim)
{
int ans=point[aim].x+point[aim].y;
if(ans<tree[n].index)
tree[n].index=ans;
if(l==r)
{
if(ans==tree[n].index)
tree[n].current=aim;
return;
}
if(mid<current)
update(mid+,r,n<<|,current,aim);
else update(l,mid,n<<,current,aim);
} void search(int l,int r,int n,int current,int aim)
{
if(l==r)
{
if(tree[n].index<temp1)
{
temp1=tree[n].index;
temp2=tree[n].current;
}
return;
}
if(mid<current)
search(mid+,r,n<<|,current,aim);
else if(l>=current)
{
if(tree[n<<].index<tree[n<<|].index)
search(l,mid,n<<,current,aim);
else search(mid+,r,n<<|,current,aim);
}
else
{
if(tree[n<<].index>=tree[n<<|].index)
search(mid+,r,n<<|,current,aim);
else
{
search(l,mid,n<<,current,aim);
search(mid+,r,n<<|,current,aim);
}
}
} int find(int x)
{
int temp=x;
while(x!=fa[x])
x=fa[x];
while(temp!=fa[temp])
{
temp=fa[temp];
fa[temp]=x;
}
return x;
} int pos[];
bool sign[];
int ans[]; int main()
{
int n,k;
while(cin>>n>>k)
{
int m=;
ent=;
memset(sign,false,sizeof(sign));
for(int i=; i<n; i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
point[i].id=i;
}
for(int tot=; tot<; tot++)
{
if(tot==)
{
for(int i=; i<n; i++)
point[i].y=-point[i].y;
}
if(tot==||tot==)
{
for(int i=; i<n; i++)
{
point[i].x=point[i].x+point[i].y;
point[i].y=point[i].x-point[i].y;
point[i].x=point[i].x-point[i].y;
}
}
for(int i=; i<n; i++)
{
int temp=get_ans(point[i]);
if(!sign[temp])
{
pos[m++]=temp;
sign[temp]=true;
}
}
sort(pos,pos+m);
for(int i=; i<m; i++)
{
ans[pos[i]]=i;
}
sort(point,point+n,cmp);
build(,m-,);
for(int i=n-; i>; i--)
{
update(,m-,,ans[get_ans(point[i])],i);
temp1=INT_MAX;
search(,m-,,ans[get_ans(point[i-])],i-);
if(temp1!=INT_MAX)
{
edge[ent].s=point[i-].id,edge[ent].t=point[temp2].id;
edge[ent++].dis=get_dis(i-,temp2);
}
}
}
sort(edge,edge+ent,cmp2);
for(int i=; i<=n; i++)
fa[i]=i;
int tot=;
for(int i=; i<ent; i++)
{
int x=find(edge[i].s);
int y=find(edge[i].t);
if(x==y)
continue;
else
{
fa[y]=x;
tot++;
}
if(tot==n-k)
{
cout<<edge[i].dis<<endl;
break;
}
}
}
return ;
}

poj3241 曼哈顿最小距离生成树第k大的边的更多相关文章

  1. POJ3241 最小曼哈顿距离生成树 - 真有趣哇

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  给你n个坐标, ...

  2. poj 2349 Arctic Network(最小生成树的第k大边证明)

    题目链接: http://poj.org/problem?id=2349 题目大意: 有n个警戒部队,现在要把这n个警戒部队编入一个通信网络, 有两种方式链接警戒部队:1,用卫星信道可以链接无穷远的部 ...

  3. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  4. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  5. 区间第K大(一)

    Problem: 给定无序序列S:[b, e),求S中第K大的元素. Solution 1.裸排序 2.现将区间均分成两段,S1, S2,对S1,S2分别排序,然后

  6. 寻找数组中的第K大的元素,多种解法以及分析

    遇到了一个很简单而有意思的问题,可以看出不同的算法策略对这个问题求解的优化过程.问题:寻找数组中的第K大的元素. 最简单的想法是直接进行排序,算法复杂度是O(N*logN).这么做很明显比较低效率,因 ...

  7. [51nod1685]第k大区间

    Description 定义一个长度为奇数的区间的值为其所包含的的元素的中位数. 现给出$n$个数,求将所有长度为奇数的区间的值排序后,第$k$大的值为多少. Input 第一行两个数$n$和$k$. ...

  8. 数据结构2 静态区间第K大/第K小

    给定数组$A[1...N]$, 区间$[L,R]$中第$K$大/小的数的指将$A[L...R]$中的数从大到小/从小到大排序后的第$K$个. "静态"指的是不带修改. 这个问题有多 ...

  9. POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)

    传送门 The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8690   Acce ...

随机推荐

  1. SQL Server Profiler工具

    一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问 ...

  2. numpy.distutils.system_info.NotFoundError: no lapack/blas resources found

    python35用pip安装scipy的时候报错 numpy.distutils.system_info.NotFoundError: no lapack/blas resources found 原 ...

  3. gulp.js简单操作

    一.安装gulp 1.深入设置任务之前,需先安装gulp: $ npm install gulp -g 2.这会将gulp安装到全域环境下,让你可以存取gulp的CLI.接著,需要在本地端的专案进行安 ...

  4. Win7 远程桌面 错误代码:5 异常处理(您的远程桌面会话即将结束 此计算机的虚拟内存可能不足。请关闭其他程序,然后重试连接远程计算机。如果问题仍然存在,请联系网络管理员或技术支持。)

    问题表现: 在用windows7 远程桌面连接其他电脑时,出现错误提示对话框—-标题为“严重错误(错误代码:5)”,内容为“您的远程桌面会话即将结束 此计算机的虚拟内存可能不足.请关闭其他程序,然后重 ...

  5. C#微信公众号开发-MVC模式公共类封装

    第一部分:基础配置 第一步:注册微信公众账号 如果开发测试阶段可以打开测试链接地址,注册测试公众号.测试账号除了不能与正式账号通信外其他什么高级接口的都可以实现. 测试号管理地址:http://mp. ...

  6. python---PrettyTable

    简介 Python通过prettytable模块将输出内容如表格方式整齐输出,python本身并不内置,需要独立安装该第三方库. 安装 pip install PrettyTable #源码安装 wg ...

  7. maya,mel,eval,stringarray

    mel里,当要eval("client()"),并且要传递stirngarray参数给函数client()时,正确的写法应该是: global proc intermediator ...

  8. Celery 和 Redis 入门

    Celery 是一个广泛应用于网络应用程序的任务处理系统. 它可以在以下情况下使用: 在请求响应周期中做网络调用.服务器应当立即响应任何网络请求.如果在请求响应周期内需要进行网络调用,则应在周期外完成 ...

  9. Graph单元

    感谢世外苏子恒同学提供   一.调用单元 例:uses graph;   二.初始化 例:initgraph(var graphdriver,graphmode:integer; const path ...

  10. [DFNews] GetData也出取证软件了

    从事计算机取证的应该都听说过MIP(Mount Image Pro).VFC仿真和Recover My Files,上述三个应用比较广泛的软件都是GetData公司的产品.GetData现在也推出了自 ...