E - Cosmic Rays
Time limit : 2sec / Memory limit : 256MB
Score : 600 points
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.
- −109≤xs,ys,xt,yt≤109
- (xs,ys) ≠ (xt,yt)
- 1≤N≤1,000
- −109≤xi,yi≤109
- 1≤ri≤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 10−9.
Sample Input 1
-2 -2 2 2 1 0 0 1
Sample Output 1
3.6568542495
An optimal route is as follows:
![e9c630751968b7051df5750b7ddc0e07.png](https://i-blog.csdnimg.cn/blog_migrate/d2ce3bb35729b7c2aa6945e1231c3216.png)
Sample Input 2
-2 0 2 0 2 -1 0 2 1 0 2
Sample Output 2
0.0000000000
An optimal route is as follows:
![fb82f6f4df5b22cffb868ce6333277aa.png](https://i-blog.csdnimg.cn/blog_migrate/51cdbb4068b1a0349028ed72fc2e7a41.png)
Sample Input 3
4 -2 -2 4 3 0 0 2 4 0 1 0 4 1
Sample Output 3
4.0000000000
An optimal route is as follows:
![d09006720c225cbe69eae3fd9c186e67.png](https://i-blog.csdnimg.cn/blog_migrate/236b0a788da2f9bf50a9310cbfc39559.png)
题意:给出两点,找出从一点到另一点的经过的非圆内区域的最小值。
题解:将所有圆变成点,点与点之间的距离等于他们圆心之间的距离减去他们的半径和,然后再用SPFA求最短路。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 20;
int vis[maxn],n;
double x[maxn],y[maxn],r[maxn],dist[maxn],mp[maxn][maxn];
void SPFA(int s)
{
queue<int>q;
for(int i = 1;i <= n;i++) dist[i] = 1e10;
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
dist[s] = 0;
vis[s] = 1;
q.push(s);
while(!q.empty())
{
int v = q.front();
q.pop();
vis[v] = 0;
for(int i = 1;i <= n;i++)
{
if(dist[i] > dist[v] + mp[v][i])
{
dist[i] = dist[v] + mp[v][i];
if(!vis[i])
{
vis[i] = 1;
q.push(i);
}
}
}
}
}
int main()
{
while(scanf("%lf%lf%lf%lf",&x[1],&y[1],&x[2],&y[2]) != EOF)
{
scanf("%d",&n);
n += 2;
r[1] = r[2] = 0;
mp[1][2] = mp[2][1] = sqrt((x[1] - x[2]) * (x[1] - x[2]) + (y[1] - y[2]) * (y[1] - y[2]));
for(int i = 3;i <= n;i++)
{
scanf("%lf%lf%lf",&x[i],&y[i],&r[i]);
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
mp[i][j] = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
mp[i][j] -= r[i] + r[j];
if(mp[i][j] < 0)
{
mp[i][j] = 0;
}
}
}
SPFA(1);
printf("%.10f\n",dist[2]);
}
return 0;
}