Advertisement
erfanul007

LOJ 1240

Jun 23rd, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdlib.h>
  4.  
  5. ///constant
  6. #define eps 1e-6
  7. #define max 1e9
  8.  
  9. struct point{
  10. double x,y,z;
  11.  
  12. }A,B,P,temp;
  13.  
  14. struct point Scan(){scanf("%lf %lf %lf",&temp.x,&temp.y,&temp.z);return temp;}
  15.  
  16. double SQR(double p){return p*p;}
  17.  
  18. double differ(struct point P,struct point Q){return SQR(P.x-Q.x)+SQR(P.y-Q.y)+SQR(P.z-Q.z);}
  19.  
  20. struct point DIV(struct point P,struct point Q,double m,double n)
  21. {
  22. temp.x=(m*Q.x+n*P.x)/(m+n);
  23. temp.y=(m*Q.y+n*P.y)/(m+n);
  24. temp.z=(m*Q.z+n*P.z)/(m+n);
  25. return temp;
  26. }
  27.  
  28. double min(double x,double y){return (x<y)?x:y;}
  29.  
  30. double Ternarysearch(struct point loAB,struct point hiAB,struct point P,int depth)
  31. {
  32. if(depth>50){
  33. return max;
  34. }
  35. struct point midAB1,midAB2;
  36. double diff1,diff2,diff;
  37. midAB1=DIV(loAB,hiAB,1,2);
  38. midAB2 = DIV(midAB1,hiAB,1,1);
  39. diff1 = differ(midAB1,P);
  40. diff2 = differ(midAB2,P);
  41. if(diff1<diff2){
  42. diff = Ternarysearch(loAB,midAB2,P,depth+1);
  43. }
  44. else{
  45. diff = Ternarysearch(midAB1,hiAB,P,depth+1);
  46. }
  47. return min(min(diff,diff1),min(diff,diff2));
  48. }
  49.  
  50. int main()
  51. {
  52. //freopen("input.txt","r",stdin);
  53. //freopen("output.txt","w",stdout);
  54. int t;
  55. scanf("%d",&t);
  56. for(int i=1;i<=t;i++){
  57. A=Scan();B=Scan();P=Scan();;
  58. printf("Case %d: %lf\n",i,sqrt(Ternarysearch(A,B,P,0)));
  59. }
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement