Typhoon 2023“钉耙编程”中国大学生算法设计超级联赛(5)hdu7324

Problem - 7324

题目大意:直角坐标系内有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;
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timidcatt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值