Cosmic Rays(最短路变形)

时间限制: 1 Sec   内存限制: 128 MB

题目描述

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

输入

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

输出

Print the minimum possible duration of time Snuke is exposed to cosmic rays during the travel. (精确到小数点后9位)

样例输入

-2 -2 2 2
1
0 0 1

样例输出

3.656854249

提示

An optimal route is as follows:


来源


首先要看出这是最短路,然后处理每个点之间的距离等于圆心的距离减两个半径
如果maze是负数,就变成0;

AC 代码

#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);
    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;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1010;
int n;
const long double inf=1e37;
struct node
{
    int x,y,r;
}s[maxn];
double maze[maxn][maxn];
double dis[maxn];
bool vis[maxn];
void dijkstra(int s)
{
    dis[s]=0;
    while(1){
        int v=-1;
        for(int i=0;i<=n+1;i++){
            if(!vis[i]&&(v==-1||dis[i]<dis[v])) v=i;
        }
        if(v==-1) break;
        vis[v]=1;
        for(int i=0;i<=n+1;i++)
        dis[i]=min(dis[i],dis[v]+maze[v][i]);
    }
}
int main()
{
    int enx,eny;
    scanf("%d%d%d%d",&s[0].x,&s[0].y,&enx,&eny);
    scanf("%d",&n);
    s[n+1].x=enx;
    s[n+1].y=eny;
    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]=(double)sqrt((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];
        }
    }
    for(int i=0;i<=n+1;i++)
        dis[i]=inf;
    dijkstra(0);
    printf("%.9lf\n",dis[n+1]);
    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_41021816/article/details/80515630