6487: Cosmic Rays
时间限制: 1 Sec 内存限制: 128 MB提交: 76 解决: 37
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
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
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
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:

问题:给定起点和终点,其中会经过n堡垒,每个堡垒会有半径为r的保护罩,有保护伞的地方不会受到宇宙辐射,没有保护罩的地方会受到宇宙射线的辐射。飞机速度为1,求飞机最短的受到宇宙辐射的路程。
分析:把终点起点填加进来,跑一个最短路,用long double。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
struct node
{
int x,y,r;
}e[1005];
int n,sx,sy,ex,ey;
long double mp[1005][1005],dis[1005];
int vis[MAX];
void dijkstra()
{
int i,j,indx;
long double temp;
memset(vis,0,sizeof(vis));
vis[1]=1;
for(i=1;i<=n;i++)
dis[i]=mp[1][i];
dis[1]=0;
for(i=2;i<=n;i++)
{
indx=0;
temp=1e18;
for(j=1;j<=n;j++)
{
if(temp>dis[j] && !vis[j])
{
indx=j;
temp=dis[j];
}
}
if(!indx)
break;
vis[indx]=1;
for(j=1;j<=n;j++)
{
if(dis[j]>dis[indx]+mp[indx][j] && !vis[j])
dis[j]=mp[indx][j]+dis[indx];
}
}
printf("%.9Lf\n",dis[n]);
}
int main()
{
int i,j;
for(i=0;i<1005;i++)
e[i].r=0;
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
scanf("%d",&n);
for(i=2;i<=n+1;i++)
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].r);
n=n+2;
e[n].x=ex;
e[n].y=ey;
e[1].x=sx;
e[1].y=sy;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
mp[i][j]=(long double)sqrt((long double)(e[i].x-e[j].x)*(e[i].x-e[j].x)+(long double)(e[i].y-e[j].y)*(e[i].y-e[j].y));
mp[i][j]=mp[i][j]-e[i].r-e[j].r;
if(mp[i][j]<0)
mp[i][j]=0;
mp[j][i]=mp[i][j];
}
}
dijkstra();
return 0;
}