题目大意:直角坐标系内有n个点连接成固定的n-1条线段,另有m个点,问每个点距离最近的线段的距离是多少
2<=m,m<=1e4;-1e9<=x,y<=1e9
思路:直接对于每个点遍历所有线段,求出距离后维护最小值即可
tips:hdu oj 1s=1e8,没有special judge,纯靠文本比较,所以精度要求很高,能用整数用整数,浮点数用long double
//#include<__msvc_all_public_headers.hpp>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
// 设线段两段点A(x1,y1) B (x2 , y2) 设顶点 C ( x , y );
long double cn(ll x, ll y, ll x1, ll y1, ll x2, ll y2)
{
long double cross = (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1); // |AB| * |AC|*cos(x)
if (cross <= 0) //积小于等于0,说明 角BAC 是直角或钝角
return sqrtl((x - x1) * (x - x1) + (y - y1) * (y - y1) );
long double d2 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); // |AB|
if (cross >= d2) //角ABC是直角或钝角
return sqrtl((x - x2) * (x - x2) + (y - y2) * (y - y2) );
//锐角三角形
long double r = cross / d2;
long double px = x1 + (x2 - x1) * r; // C在 AB上的垂足点(px,py)
long double py = y1 + (y2 - y1) * r;
return sqrtl((x - px) * (x - px) + (y - py) * (y - py) ); //两点间距离公式
}
const int N = 1e4 + 5;
pair<int, int>a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i].first >> a[i].second;
}
for (int i = 1; i <= m; i++)
{
long double mi = 5e9;
int x, y;
cin >> x >> y;
for (int i = 1; i < n; i++)
{
mi = min(mi, cn(x, y, a[i].first, a[i].second, a[i + 1].first, a[i + 1].second));
}
cout << fixed << setprecision(4)<<mi << endl;
}
return 0;
}