First of all most likely to think violence is an enumeration of all segments N2 to find the minimum, but it will do a lot of wasted effort. We can first sorting line, such that the leftmost end in accordance with the x-axis y-axis segment ordering, then we can define all segments in the segment of the rectangular frame may have only a minimum value, for each query i-th segment the closest distance, if the difference x x left-most point of the j-th segment of the i-th right-most point of the segment is greater than ans, it can directly break, after the enumeration is no meaning, must be greater than ans, so the addition of this part of the pruning complexity to compress a large part.
1 // ——By DD_BOND 2 3 //#include<bits/stdc++.h> 4 //#include<unordered_map> 5 //#include<unordered_set> 6 #include<functional> 7 #include<algorithm> 8 #include<iostream> 9 //#include<ext/rope> 10 #include<iomanip> 11 #include<climits> 12 #include<cstring> 13 #include<cstdlib> 14 #include<cstddef> 15 #include<cstdio> 16 #include<memory> 17 #include<vector> 18 #include<cctype> 19 #include<string> 20 #include<cmath> 21 #include<queue> 22 #include<deque> 23 #include<ctime> 24 #include<stack> 25 #include<map> 26 #include<set> 27 28 #define fi first 29 #define se second 30 #define MP make_pair 31 #define pb push_back 32 33 #pragma GCC optimize(3) 34 #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 35 36 using namespace std; 37 38 typedef long long ll; 39 40 const int MAXN=1e5+10; 41 const double eps=1e-8; 42 const double pi=acos(-1.0); 43 const ll INF=0x3f3f3f3f3f3f3f3f; 44 45 inline int dcmp(double x){ 46 if(fabs(x)<eps) return 0; 47 return (x>0? 1: -1); 48 } 49 50 inline double sqr(double x){ return x*x; } 51 52 struct Point{ 53 double x,y; int id; 54 Point(){ x=0,y=0; } 55 Point(double _x,double _y):x(_x),y(_y){} 56 void input(){ scanf("%lf%lf",&x,&y); } 57 void output(){ printf("%.2f %.2f\n",x,y); } 58 inline bool operator <(const Point &b)const{ 59 return (dcmp(x-b.x)==0? dcmp(y-b.y)<0 : x<b.x); 60 } 61 inline Point operator -(const Point &b)const{ 62 returnPoint (xb.x, Y- by); 63 is } 64 inline Double LEN2 () { // square of the length 65 return SQR (X) + SQR (Y); 66 } 67 inline Double len () { // length 68 return sqrt (LEN2 ()); 69 } 70 }; 71 is 72 inline Double Cross (Point A, Point B) { // cross product 73 is return AX-AY * * by BX; 74 } 75 76 inlineDouble DOT (Point A, Point B) { // dot product 77 return AX + AY * * b.x by; 78 } 79 80 inline Double DIS (Point A, Point B) { // distance between two points 81 Point P BA =; return p.len (); 82 } 83 84 struct Line { 85 Point S, E; 86 Line () {} 87 Line (Point _s, _E Point): S (_s), E (_E) {} // two points define a line 88 void INPUT () { 89 s.input (); 90 e.input (); 91 is } 92 Double length () { 93 return DIS (S, E); 94 } 95 }; 96 97 inline Double point_to_line (Point P, Line A) { // point to the straight line distance 98 return FABS (Cross (Pa.s, AE-AS) / a.length ()); 99 } 100 101 inline Double point_to_seg (point P, line A) { // points to the line segment 102 IF (dCMP (dOT (Pa.s , AE-AS)) < 0 || dCMP (DOT (pa.e, AS-AE)) < 0 ) 103 return min(dis(p,a.e),dis(p,a.s)); 104 return point_to_line(p,a); 105 } 106 107 inline double seg_to_seg(Line u,Line v){ 108 return min( min(point_to_seg(u.s,v),point_to_seg(u.e,v)), min( point_to_seg(v.s,u),point_to_seg(v.e,u)) ); 109 } 110 111 Line line[MAXN]; 112 113 bool cmp(Line a,Line b){ 114 return a.s<b.s; 115 } 116 117 int main(void){ 118 int T; scanf("%d",&T); 119 while(T--){ 120 int n; scanf("%d",&n); 121 for(int i=0;i<n;i++){ 122 line[i].input(); 123 if(line[i].e<line[i].s) swap(line[i].s,line[i].e); 124 } 125 sort(line,line+n,cmp); 126 double ans=1e10; 127 for(int i=0;i<n;i++) 128 for(int j=i+1;j<n;j++){ 129 if(dcmp(line[j].s.x-line[i].e.x-ans)>0) break; 130 ans=min(ans,seg_to_seg(line[i],line[j])); 131 } 132 printf("%.12f\n",ans); 133 } 134 return 0; 135 }