2019 杭电多校 10 1007
题目链接:HDU 6697
比赛链接:2019 Multi-University Training Contest 10
Problem Description
The closest pair of points problem is a well-known problem of computational geometry. In this problem, you are given $n$ points in the Euclidean plane and you need to find a pair of points with the smallest distance between them.
Now, Claris, the brilliant one who has participated in programming contests for several years, is trying to solve a harder problem named the closest pair of segments problem, which also has a quite simple description as above.
However, the problem seems even too hard for Claris and she is asking you for help.
Now $n$ segments are lying on the Euclidean plane, you are asked to pick two different segments and then pick a point on the two segments respectively to minimize the distance between these two points.
For simplicity, any two given segments share no common point, and you don’t need to show her the two chosen points, but the distance between them instead.
Input
The input contains several test cases, and the first line contains a single integer $T (1\le T\le 200)$, the number of test cases.
For each test case, the first line contains one integer $n (2\le n\le 10000)$, which is the number of segments on the Euclidean plane.
The following $n$ lines describe all the segments lying on the Euclidean plane, the $i$-th of which contains for integers $x_1,y_1,x_2$ and $y_2$ describing a segment that connects $(x_1,y_1)$ and $(x_2,y_2)$, where $−10^9\le x_1,y_1,x_2,y_2\le 10^9$.
It’s guaranteed that the two endpoints of each segment do not coincide, any two given segments do not intersect with each other in each test case, and no more than $20$ test cases satisfy $n>1000$.
Output
For each test case, output a line containing a single real number for the answer to the closest pair of segments problem with an absolute or relative error of at most $10^{−6}$.
Precisely speaking, assume that your answer is $a$ and and the jury’s answer is $b$, your answer will be considered correct if and only if $\frac{|a−b|}{max\{1,|b|\}}\le 10^{−6}$.
Sample Input
1 | 2 |
Sample Output
1 | 0.707106781187 |
Solution
题意
类似于计算几何中的最近点对问题,本题求的是最近线段对。
给定 $n$ 条线段,求出最近线段对之间的距离。
题解
暴力 剪枝
比赛时我用了三角剖分,结果超时了。
赛后补题时看到了这篇博客:HDU 6697 Closest Pair of Segments(线段距离)
原来暴力加上剪枝就能过。思路是这样的:
首先将线段的左侧端点按照横坐标为第一关键字,纵坐标为第二关键字排序。然后暴力找所有线段对,维护最小值 $ans$。如果当前查询的线段对中,右侧线段的左端点与左侧线段的右端点的横坐标差值大于 $ans$ 时,就不用再找更右侧的直线了。这样剪枝能大大减少时间复杂度。
时限给了 20s,大概 1.3s 就能跑完。
题解看不懂
Code
1 |
|