AtCoder - 2154 Cosmic Rays

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.
  • −109xs,ys,xt,yt109
  • (xs,ys)(xt,yt)
  • 1N1,000
  • −109xi,yi109
  • 1ri109

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

Copy
-2 -2 2 2
1
0 0 1

Sample Output 1

Copy
3.6568542495

An optimal route is as follows:

e9c630751968b7051df5750b7ddc0e07.png

Sample Input 2

Copy
-2 0 2 0
2
-1 0 2
1 0 2

Sample Output 2

Copy
0.0000000000

An optimal route is as follows:

fb82f6f4df5b22cffb868ce6333277aa.png

Sample Input 3

Copy
4 -2 -2 4
3
0 0 2
4 0 1
0 4 1

Sample Output 3

Copy
4.0000000000

An optimal route is as follows:

d09006720c225cbe69eae3fd9c186e67.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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值