您的位置:首页 > 其它

CodeForces-89D:Space mines(思维)

2017-09-01 09:28 393 查看
D. Space mines

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Once upon a time in the galaxy of far, far away...

Darth Wader found out the location of a rebels' base. Now he is going to destroy the base (and the whole planet that the base is located at), using the Death Star.

When the rebels learnt that the Death Star was coming, they decided to use their new secret weapon — space mines. Let's describe a space mine's build.

Each space mine is shaped like a ball (we'll call it the mine body) of a certain radius r with the center in the point O.
Several spikes protrude from the center. Each spike can be represented as a segment, connecting the center of the mine with some point P,
such that 

 (transporting
long-spiked mines is problematic), where |OP| is the length of the segment connecting O and P.
It is convenient to describe the point P by a vector p such
that P = O + p.

The Death Star is shaped like a ball with the radius of R (R exceeds
any mine's radius). It moves at a constant speed along the v vector at the speed equal to |v|.
At the moment the rebels noticed the Star of Death, it was located in the point A.

The rebels located n space mines along the Death Star's way. You may regard the mines as being idle. The Death Star does not know about
the mines' existence and cannot notice them, which is why it doesn't change the direction of its movement. As soon as the Star of Death touched the mine (its body or one of the spikes), the mine bursts and destroys the Star of Death. A touching is the situation
when there is a point in space which belongs both to the mine and to the Death Star. It is considered that Death Star will not be destroyed if it can move infinitely long time without touching the mines.

Help the rebels determine whether they will succeed in destroying the Death Star using space mines or not. If they will succeed, determine the moment of time when it will happen (starting from the moment the Death Star was noticed).

Input

The first input data line contains 7 integers Ax, Ay, Az, vx, vy, vz, R.
They are the Death Star's initial position, the direction of its movement, and its radius ( - 10 ≤ vx, vy, vz ≤ 10, |v| > 0, 0 < R ≤ 100).

The second line contains an integer n, which is the number of mines (1 ≤ n ≤ 100).
Then follow n data blocks, the i-th
of them describes the i-th mine.

The first line of each block contains 5 integers Oix, Oiy, Oiz, ri, mi,
which are the coordinates of the mine centre, the radius of its body and the number of spikes (0 < ri < 100, 0 ≤ mi ≤ 10).
Then follow mi lines,
describing the spikes of the i-th mine, where the j-th
of them describes the i-th spike and contains 3 integers pijx, pijy, pijz —
the coordinates of the vector where the given spike is directed (

).

The coordinates of the mines' centers and the center of the Death Star are integers, their absolute value does not exceed 10000. It is guaranteed
that R > ri for
any 1 ≤ i ≤ n. For any mines i ≠ j the
following inequality if fulfilled: 

.
Initially the De
10bc4
ath Star and the mines do not have common points.

Output

If the rebels will succeed in stopping the Death Star using space mines, print the time from the moment the Death Star was noticed to the blast.

If the Death Star will not touch a mine, print "-1" (without quotes).

For the answer the absolute or relative error of 10 - 6 is
acceptable.

Examples

input
0 0 0 1 0 0 5
2
10 8 0 2 2
0 -3 0
2 2 0
20 0 0 4 3
2 4 0
-4 3 0
1 -5 0


output
10.0000000000


input
8 8 4 4 4 2 6
1
-2 -2 -1 3 0


output
-1


input
30 30 2 1 2 1 20
3
0 0 40 5 1
1 4 4
-10 -40 -5 7 0
100 200 95 8 1
-10 0 0


output
74.6757620881


题意:给你一个球,和一些地雷,这些地雷表层有类似刺一样的突起,这个球碰到地雷或者地雷上的刺就会爆炸,求爆炸发生的时间。如果永远不会爆炸,输出-1;

思路:先不考虑地雷的刺。把这个球看成一个点(即球心),把地雷半径增加R(球的半径),那么当点沿着v(球的速度矢量)运动的路径,即一条射线,与地雷有交点时,球就会爆炸。

现在来考虑地雷上的突刺。同上,我们把球看成一个点,把那根突刺变为一个半径为R的圆柱,即向周围延伸R的长度,同时上底面和下底面加上一个半径为R的半球。同理,当点运动的路径与圆柱有交点时,球就会爆炸。

我们可以发现,这个圆柱除了上底面那个半球,其它部分都被包在地雷里面(地雷半径为R+r)。证明如下:

 


                                                        


r0是突刺的长度。
那么球的路径只可能与圆柱的上底面那个半球有交点。如果与其它地方有交点,那么在此之前一定会与地雷有交点。
此时我们只需要找出半径增加R后的地雷与球的球心运动路径相交的最早时间,以及与以突刺端点为球心,R为半径的球相交的最早时间,然后取最小值即可。

                                            (Ax + vxt - Ox)2 + (Ay + vyt - Oy)2 + (Az + vzt - Oz)2 = R2.

即求解这个方程的2个根。A为球心,v为球的速度,O为地雷球心和突刺端点。
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
struct Point
{
double x,y,z;
};
struct ball
{
double x,y,z,r;
vector<Point>a;
}p[1000],v;
int main()
{
int n;
scanf("%lf%lf%lf%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[0].z,&v.x,&v.y,&v.z,&p[0].r);
scanf("%d",&n);
for(int i=1,m;i<=n;i++)
{
scanf("%lf%lf%lf%lf%d",&p[i].x,&p[i].y,&p[i].z,&p[i].r,&m);
p[i].r+=p[0].r;
while(m--)
{
double x,y,z;
scanf("%lf%lf%lf",&x,&y,&z);
p[i].a.push_back((Point){x+p[i].x,y+p[i].y,z+p[i].z});
}
}
double ans=1e9+7;
for(int i=1;i<=n;i++)
{
double A=v.x*v.x+v.y*v.y+v.z*v.z;
double B=2*((p[0].x-p[i].x)*v.x+(p[0].y-p[i].y)*v.y+(p[0].z-p[i].z)*v.z);
double C=(p[0].x-p[i].x)*(p[0].x-p[i].x)+(p[0].y-p[i].y)*(p[0].y-p[i].y)+(p[0].z-p[i].z)*(p[0].z-p[i].z)-p[i].r*p[i].r;
double D=B*B-4*A*C;
if(D>=0)
{
double x1=(-B-sqrt(D))/(2*A);
double x2=(-B+sqrt(D))/(2*A);
if(x1>0)ans=min(ans,x1);
if(x2>0)ans=min(ans,x2);
}
for(int j=0;j<p[i].a.size();j++)
{
Point b=p[i].a[j];
A=v.x*v.x+v.y*v.y+v.z*v.z;
B=2*((p[0].x-b.x)*v.x+(p[0].y-b.y)*v.y+(p[0].z-b.z)*v.z);
C=(p[0].x-b.x)*(p[0].x-b.x)+(p[0].y-b.y)*(p[0].y-b.y)+(p[0].z-b.z)*(p[0].z-b.z)-p[0].r*p[0].r;
D=B*B-4*A*C;
if(D>=0)
{
double x1=(-B-sqrt(D))/(2*A);
double x2=(-B+sqrt(D))/(2*A);
if(x1>0)ans=min(ans,x1);
if(x2>0)ans=min(ans,x2);
}
}
}
if(ans>=1e9)puts("-1");
else printf("%0.10lf\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: