题目链接:Cosmic Rays
Problem Statement
On the
xy-plane, Snuke is going to travel from the point (xs,ys) to the point (xt,yt). He can move in arbitrary directions with speed 1. Here, we will consider him as a point without size.
There are N circular barriers deployed on the plane. The center and the radius of the i-th barrier are (xi,yi) and ri, respectively. The barriers may overlap or contain each other.
A point on the plane is exposed to cosmic rays if the point is not within any of the barriers.
Snuke wants to avoid exposure to cosmic rays as much as possible during the travel. Find the minimum possible duration of time he is exposed to cosmic rays during the travel.
Constraints
All input values are integers.
−
1
0
9
−10^9
−109≤xs,ys,xt,yt≤
1
0
9
10^9
109
(xs,ys) ≠ (xt,yt)
1≤N≤1,000
−
1
0
9
−10^9
−109≤xi,yi≤
1
0
9
10^9
109 1≤ri≤
1
0
9
10^9
109
Input
The input is given from Standard Input in the following format:
xs ys xt yt
N
x1 y1 r1
x2 y2 r2
:
xN yN rN
Output
Print the minimum possible duration of time Snuke is exposed to cosmic rays during the travel. The output is considered correct if the absolute or relative error is at most − 1 0 9 −10^9 −109
题意
需要从点(xs,ys)到达点(xt,yt),途中会有辐射,同时在地图上会有N个圆,已知各个圆的圆心坐标和半径,并且在圆中行走不会受到辐射,人行走的速度为1,问途中受到辐射的最短时间。
题解
首先需要明白两点之间直线最短,所以受到辐射最短时间的方法是往着各个圆心的方向行走。
故可以把起点、终点和圆心从二维理解成一维的各个点,两点之间的受到辐射的时间是权值,且权值等于两点之间是距离差-两点之间的半径和,即
(
x
i
−
x
j
)
2
+
(
y
i
−
y
j
)
2
−
(
r
i
+
r
j
)
\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}-(r_i+r_j)
(xi−xj)2+(yi−yj)2−(ri+rj)
把点(xs,ys)理解成点1,把点点(xt,yt)理解成点n+2,再此基础上以1为根跑dij最短路,求出到点n+2的距离即是答案。
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
const int MAX=20050;
const ll inf=0x3f3f3f3f3f;
struct point{
long double x,y,r;
}a[MAX];
long double dis[MAX];
int vis[MAX];
ll n,m,point;// 点数 边数 起始点
vector<pair<ll,long double> >v[MAX];
struct node
{
ll id;
long double d;
node(){};
node(int id,long double d):id(id),d(d){};
bool operator <(const node&a)const{
return d>a.d;
}
};
void dijkstra(int point)
{
for(int i=1;i<=n+2;i++){
vis[i]=0;dis[i]=1.00*inf;
}
dis[point]=0;
priority_queue<node>q; //大根堆,优先队列在logn内可以从大到小排序
q.push(node(point,0));
node temp;
while(!q.empty())
{
temp=q.top();q.pop();
if(vis[temp.id]==1)continue;
vis[temp.id]=1;
for(int i=0;i<v[temp.id].size();i++)
{
int j=v[temp.id][i].first;
long double k=v[temp.id][i].second;
if(temp.d+k<dis[j]&&vis[j]==0)
{
dis[j]=temp.d+k;
q.push(node(j,dis[j]));
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
ll xs,ys,xt,yt;cin>>xs>>ys>>xt>>yt;
cin>>n;
a[1].x=xs;a[1].y=ys,a[1].r=0;
for(int i=2;i<=n+1;i++)
cin>>a[i].x>>a[i].y>>a[i].r;
a[n+2].x=xt;a[n+2].y=yt,a[n+2].r=0;
for(int i=1;i<=n+2;i++){
for(int j=i+1;j<=n+2;j++){
long double s=(long double)sqrt(1.00*(a[i].x-a[j].x)*(a[i].x-a[j].x)+1.00*(a[i].y-a[j].y)*(a[i].y-a[j].y))-1.00*a[i].r-1.00*a[j].r;
if(s<0)s=0;
v[i].push_back(make_pair(j,s));
v[j].push_back(make_pair(i,s));
}
}
point=1;
dijkstra(point);
cout<<fixed<<setprecision(9)<<dis[n+2]<<endl;
return 0;
}