E - Cosmic Rays(dj最短路)

Problem Statement

On the xxyy-plane, Snuke is going to travel from the point (xs,ys)(xs,ys) to the point (xt,yt)(xt,yt). He can move in arbitrary directions with speed 11. Here, we will consider him as a point without size.

There are NN circular barriers deployed on the plane. The center and the radius of the ii-th barrier are (xi,yi)(xi,yi) and riri, 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−109≤xs,ys,xt,yt≤109
  • (xs,ys)(xs,ys) ≠ (xt,yt)(xt,yt)
  • 1≤N≤1,0001≤N≤1,000
  • −109≤xi,yi≤109−109≤xi,yi≤109
  • 1≤ri≤1091≤ri≤109

Input

The input is given from Standard Input in the following format:

xsxs ysys xtxt ytyt
NN
x1x1 y1y1 r1r1
x2x2 y2y2 r2r2
::
xNxN yNyN rNrN

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−910−9.


Sample Input 1 Copy

Copy

-2 -2 2 2
1
0 0 1

Sample Output 1 Copy

Copy

3.6568542495

An optimal route is as follows:

e9c630751968b7051df5750b7ddc0e07.png


Sample Input 2 Copy

Copy

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

Sample Output 2 Copy

Copy

0.0000000000

An optimal route is as follows:

fb82f6f4df5b22cffb868ce6333277aa.png


Sample Input 3 Copy

Copy

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

Sample Output 3 Copy

Copy

4.0000000000

An optimal route is as follows:

d09006720c225cbe69eae3fd9c186e67.png

题意:首先给你起点和终点,

然后给你n个圆(x,y,n)让你找从起点到终点的路径中(暴露在圆外面的路径最少)输出该最少路径。

题解:

把给出的n+2个圆(起点,终点半径为0共有n+2个圆)这n+2个点中任意两点的距离初始化为(两个圆心之间的距离-两个半径如果小于0就设为0)

然后用dj单源最短路求起点到终点的最短距离就行了。

这道题可能是精度的原因自己的代码无法ac,自己找了两篇题解发现他们的代码在atcode上也是w。总的来说理解思路才是重要的。

下面是本人的代码,和网上的代码:
本人代码:

//计算图的以s点为起点的单源最短路径
//图中节点从1到n编号
//运行dijkstrea之前,需要先把图中两点间的距离保存在dist[i][j]中
//如果i到j不可达,那么dist[i][j]==INF

#include<bits/stdc++.h>
using namespace std;
const long double INF=1e37;
const int maxn=1000+5;
int n,m;//图节点数目,从1到n编号
 double d[maxn];//单源最短距离
double dist[maxn][maxn];//dist[i][j]表示i到j的有向边长
bool done[maxn];//done[i]表示d[i]是否已经计算完

//进入此函数前,需要将所有边的距离保存在dist中


struct st
{
    int x,y;
    int r;
}a[1009];
 /*long double f(int i,int j)
{
    return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y))-a[i].r-a[j].r;
}*/
void dijkstra(int s)
{
    memset(done,0,sizeof(done));
    for(int i=1;i<=n;i++) d[i]=INF;  //d[i]表示i与当前生成树中的点的的最小距离。
   d[s]=0;
    for(int i=1;i<=n;i++)
    {
        //x标记当前最短d的点,min_dist记录当前最小距离
        int x;
         double min_dist=INF;

        for(int y=1;y<=n;y++)if(!done[y] && min_dist>=d[y])
        min_dist = d[x=y];

        done[x]=true;

        for(int y=1;y<=n;y++) d[y] = min(d[y],d[x]+dist[x][y]);
    }
}

int main()
{

cin>>a[1].x>>a[1].y>>a[2].x>>a[2].y;
    a[1].r=0;
    a[2].r=0;
    cin>>n;
    n+=2;
    for(int i=3;i<=n;i++)
    {
        cin>>a[i].x>>a[i].y>>a[i].r;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)
        {
            dist[i][j]=dist[j][i]=(double)sqrt((double)(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y))-a[i].r-a[j].r;
            if(dist[i][j]<0)
            {
                dist[i][j]=dist[j][i]=0;
            }
        }
    }

dijkstra(1);
printf("%.10lf\n",d[2]);

}

 别人代码:
 

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f3f
#define N 1005
double maze[N][N],dis[N];
int n;
int vis[N];
void Dijkstra()
{
    int i,j,minn,indx;
    memset(vis,0,sizeof(vis));
    vis[0] = 1;
    for(i = 0; i<=n+1; i++)
        dis[i] = maze[0][i];
    for(i = 1; i<=n+1; i++)
    {
        minn = inf;
        for(j = 1; j<=n+1; j++)
        {
            if(dis[j]<minn && !vis[j])
            {
                indx = j;
                minn = dis[j];
            }
        }
        vis[indx] = 1;
        for(j = 1; j<=n+1; j++)
        {
            if(!vis[j] && dis[indx]+maze[indx][j]<dis[j])

            dis[j] = dis[indx]+maze[indx][j];
        }
    }
}

struct node
{
    int x;
    int y;
    int r;
} s[N];
int main()
{
    int xx,yy;

    scanf("%d%d%d%d",&s[0].x,&s[0].y,&xx,&yy);
    s[0].r=0;
    scanf("%d",&n);
    for(int i=0;i<=n+1;i++)for(int j=0;j<=n+1;j++)maze[i][j]=inf;
    s[n+1].x=xx;
    s[n+1].y=yy;
    s[n+1].r=0;
    for(int i=1; i<=n; i++)
        scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].r);

    for(int i=0; i<=n+1; i++)
    {
        for(int j=i+1; j<=n+1; j++)
        {
            maze[i][j]=(long double)sqrt((long double)(s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y));
            maze[i][j]-=s[i].r+s[j].r;
            if(maze[i][j]<0)
                maze[i][j]=0;
            maze[j][i]=maze[i][j];
        }
    }

    Dijkstra();

    printf("%.9lf\n",dis[n+1]);


    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值