需要特判一下n=1的时候
精度调太低会超时
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 310 12 #define LL long long 13 #define INF 1e8 14 const double eps = 1e-5; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 struct point 18 { 19 double x,y; 20 double vx,vy; 21 }p[N],pp[N]; 22 int n; 23 double dis(point a,point b) 24 { 25 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 26 } 27 double calc(double tt) 28 { 29 int i,j; 30 for(i = 1 ; i <= n; i++) 31 { 32 pp[i].x=p[i].x+tt*p[i].vx; 33 pp[i].y=p[i].y+tt*p[i].vy; 34 } 35 double ans = 0.0; 36 for(i = 1 ; i <= n; i++) 37 { 38 for(j = 1 ; j <= n ;j++) 39 ans = max(ans,dis(pp[i],pp[j])); 40 } 41 return ans; 42 } 43 void solve(int k) 44 { 45 double M,RM; 46 double L = 0.0; 47 double R = INF; 48 while (L + eps < R) 49 { 50 M = (L + R) / 2; 51 RM = (M + R) / 2; 52 double s1 = calc(M); 53 double s2 = calc(RM); 54 if ( s1 < s2 ) R = RM; 55 else L = M; 56 } 57 printf("Case #%d: ",k); 58 printf("%.2f %.2f ",R,calc(R)); 59 } 60 int main() 61 { 62 int t,i; 63 int kk = 0; 64 cin>>t; 65 while(t--) 66 { 67 scanf("%d",&n); 68 for(i = 1; i <= n ;i++) 69 scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].vx,&p[i].vy); 70 if(n==1) 71 { 72 printf("0.00 0.00 "); 73 continue; 74 } 75 solve(++kk); 76 } 77 return 0; 78 }