Problem - 7324 (hdu.edu.cn)
参考:
2023杭电多校5 题解 1 6 7 12 | JorbanS_JorbanS的博客-CSDN博客
题解:(计算几何)
计算每个避难处(点)到台风路径(线段)的距离
代码:
enm不太懂第二个为什么大了那么多
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod=998244353;
const int inf=1<<30;
int n,m,xx,yy;
double res;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
struct Point{
double x,y;
}a[N],P;
Point operator+(Point A,Point B){return {A.x+B.x,A.y+B.y};}
Point operator-(Point A,Point B){return {A.x-B.x,A.y-B.y};}
double operator*(Point A,Point B){return A.x*B.x+A.y*B.y;}
double operator^(Point A,Point B){return A.x*B.y-A.y*B.x;}
double len(Point P){return sqrt(P.x*P.x+P.y*P.y);}//向量OP长度
double dot(Point P,Point A,Point B){return (P-A)*(P-B);}
double cross(Point A,Point B,Point C){return (A-B)^(A-C);}
int main(){
//使其不安全的最小台风半径
m=read();n=read();
for(int i=1;i<=m;i++){
a[i].x=read();a[i].y=read();
}while(n--){
P.x=read();P.y=read();
res=1.0*inf;
for(int i=2;i<=m;i++){
Point A=a[i],B=a[i-1];
if(dot(A,P,B)<=0||dot(B,P,A)<=0){
res=min(res,min(len(P-A),len(P-B)));
}else {
res=min(res,fabs(cross(P,A,B)/len(A-B)));
}
}printf("%.4lf\n",res);
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const ll mod=998244353;
const int inf=1<<30;
const double eps=1e-7;
int n,m,xx,yy;
double res;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
struct Point{
double x,y;
Point(double _x=0,double _y=0):x(_x),y(_y){ }
}a[N],P;
typedef Point Vector;
Point operator+(Point& A,Point& B){return Point(A.x+B.x,A.y+B.y);}
Point operator-(Point& A,Point& B){return Point(A.x-B.x,A.y-B.y);}
double dot(Point& A,Point& B){return A.x*B.x+A.y*B.y;}//点乘|OA||OB|cos<OA,OB>
double cross(Point& A,Point& B){return A.x*B.y-A.y*B.x;}//叉乘
int sgn(double x){
if(fabs(x)<eps)return 0;
if(x<0)return -1;
return 1;
}
bool operator == (const Point& A,const Point& B){return !sgn(A.x-B.x)&&!sgn(A.y-B.y);}
double len(Point& A){return sqrt(dot(A,A));}
double distance_point_to_segment(Point& P,Point& A,Point& B){//P到线段AB的距离
//if(A==B)return len(P-B);//|BP|
Vector AB=B-A,AP=P-A,BP=P-B;
if(dot(AB,AP)<=0)return len(AP);//cos<AB,AP><=0钝角or直角 |AP|
if(dot(AB,BP)>0)return len(BP);//|BP|
return fabs(cross(AB,AP))/len(AB);
}
int main(){
//使其不安全的最小台风半径
m=read();n=read();
for(int i=1;i<=m;i++){
a[i].x=read();a[i].y=read();
}while(n--){
P.x=read();P.y=read();
res=1.0*inf;
for(int i=2;i<=m;i++){
double t=distance_point_to_segment(P,a[i],a[i-1]);
if(sgn(res-t)>0)res=t;
}printf("%.4lf\n",res);
}
return 0;
}
知识点:
二维几何基础大合集(一)(基础运算、点、线、多边形、圆、网格)_2d基础几何