最近点对算法模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include<algorithm> using namespace std; #define maxs 200005 struct Point { double x,y; }point[maxs]; int y_sort[maxs]; int cmpx(Point a,Point b) //按照x对这些点从小到大排序 { return a.x<b.x; } int cmpy( int a, int b) //对最近点算法中的y_sort数组排序 { return point[a].y<point[b].y; } double dis(Point a,Point b) { return sqrt ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } /* 最近点对算法,在point[first]--point[last]个点中寻找一个最短距离 使用该算法前先对这些点排序 sort(point,point+n,cmpx); */ double findMin( int first, int last) { if ((last-first)==1) return dis(point[first],point[last]); else if (last-first==2) { double d1 = dis(point[first],point[first+1]); double d2 = dis(point[first],point[first+2]); double d3 = dis(point[first+1],point[first+2]); return min(min(d1,d2),d3); } //二分 int mid = (first+last)/2; double min_dist = min(findMin(first,mid),findMin(mid+1,last)); if (min_dist==0) return 0; int y_end = 0; for ( int i=mid;point[mid].x-point[i].x<min_dist&&i>=first;i--) y_sort[y_end++]=i; for ( int i=mid+1;point[i].x-point[mid+1].x<min_dist&&i<=last;i++) y_sort[y_end++]=i; sort(y_sort,y_sort+y_end,cmpy); for ( int i=0;i<y_end;i++) for ( int j=i+1;j<y_end&&(point[y_sort[j]].y-point[y_sort[i]].y)<min_dist;j++) min_dist=min(min_dist,dis(point[y_sort[i]],point[y_sort[j]])); return min_dist; } |
poj3714
题目大意:
有若干车站和加油站,求车站和加油站的最小距离.
思路:
求最近点对,注意的是需要对车站和加油站分类,同类的话就初始化为INF
这样就不会找到相同类间的距离了,从而只会得到车站和加油站的最小距离
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <iostream> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define maxs 200005 #define INF 1e100 struct Point { double x,y; bool flag; }point[maxs]; int y_sort[maxs]; int cmpx(Point a,Point b) //按照x对这些点从小到大排序 { return a.x<b.x; } int cmpy( int a, int b) //对最近点算法中的y_sort数组排序 { return point[a].y<point[b].y; } double dis(Point a,Point b) { if (a.flag!=b.flag) return sqrt ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); return INF; //是同一类,初始化为无穷大,这样最近点对算法就不会找同类的最短距离 } /* 最近点对算法,在point[first]--point[last]个点中寻找一个最短距离 使用该算法前先对这些点排序 sort(point,point+n,cmpx); */ double findMin( int first, int last) { if ((last-first)==1) return dis(point[first],point[last]); else if (last-first==2) { double d1 = dis(point[first],point[first+1]); double d2 = dis(point[first],point[first+2]); double d3 = dis(point[first+1],point[first+2]); return min(min(d1,d2),d3); } //二分 int mid = (first+last)/2; double min_dist = min(findMin(first,mid),findMin(mid+1,last)); if (min_dist==0) return 0; int y_end = 0; for ( int i=mid;point[mid].x-point[i].x<min_dist&&i>=first;i--) y_sort[y_end++]=i; for ( int i=mid+1;point[i].x-point[mid+1].x<min_dist&&i<=last;i++) y_sort[y_end++]=i; sort(y_sort,y_sort+y_end,cmpy); for ( int i=0;i<y_end;i++) for ( int j=i+1;j<y_end&&(point[y_sort[j]].y-point[y_sort[i]].y)<min_dist;j++) min_dist=min(min_dist,dis(point[y_sort[i]],point[y_sort[j]])); return min_dist; } int main() { //freopen("in.txt","r",stdin); int T,n; cin>>T; while (T--) { scanf ( "%d" ,&n); for ( int i=0;i<n;i++) { scanf ( "%lf%lf" ,&point[i].x,&point[i].y); point[i].flag= true ; } for ( int i=n;i<2*n;i++) { scanf ( "%lf%lf" ,&point[i].x,&point[i].y); point[i].flag= false ; } n=2*n; sort(point,point+n,cmpx); //排序 double ans = findMin(0,n-1); printf ( "%0.3f\n" ,ans); } return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构