fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ld = long double;
  4.  
  5. struct P { ld x, y; };
  6.  
  7. inline P operator+(const P &a, const P &b){ return {a.x+b.x, a.y+b.y}; }
  8. inline P operator-(const P &a, const P &b){ return {a.x-b.x, a.y-b.y}; }
  9. inline P operator*(const P &a, const ld t){ return {a.x*t, a.y*t}; }
  10.  
  11. ld dist2(P a, P b){ return (a-b).x*(a-b).x + (a-b).y*(a-b).y; }
  12.  
  13. int main(){
  14. ios::sync_with_stdio(false);
  15. cin.tie(nullptr);
  16. cout << fixed << setprecision(10);
  17.  
  18. int T; cin >> T;
  19. while(T--){
  20. ld TSx, TSy, TGx, TGy, ASx, ASy, AGx, AGy;
  21. cin >> TSx >> TSy >> TGx >> TGy >> ASx >> ASy >> AGx >> AGy;
  22.  
  23. P S1={TSx,TSy}, G1={TGx,TGy};
  24. P S2={ASx,ASy}, G2={AGx,AGy};
  25.  
  26. P d1 = G1 - S1;
  27. P d2 = G2 - S2;
  28. ld L1 = sqrtl(d1.x*d1.x + d1.y*d1.y);
  29. ld L2 = sqrtl(d2.x*d2.x + d2.y*d2.y);
  30.  
  31. // 속도 벡터 (거리 / L)
  32. P v1 = (L1==0) ? P{0,0} : d1*(1.0L/L1);
  33. P v2 = (L2==0) ? P{0,0} : d2*(1.0L/L2);
  34.  
  35. ld tmax = max(L1,L2);
  36. ld left=0.0L, right=tmax;
  37.  
  38. for(int iter=0;iter<100;++iter){
  39. ld t1 = left + (right-left)/3;
  40. ld t2 = right - (right-left)/3;
  41.  
  42. P p1 = S1 + v1*(min(t1,L1));
  43. P p2 = S2 + v2*(min(t1,L2));
  44. ld d_1 = dist2(p1,p2);
  45.  
  46. p1 = S1 + v1*(min(t2,L1));
  47. p2 = S2 + v2*(min(t2,L2));
  48. ld d_2 = dist2(p1,p2);
  49.  
  50. if(d_1 < d_2) right = t2;
  51. else left = t1;
  52. }
  53.  
  54. P p1 = S1 + v1*(min(left,L1));
  55. P p2 = S2 + v2*(min(left,L2));
  56. cout << sqrtl(dist2(p1,p2)) << '\n';
  57. }
  58. }
Success #stdin #stdout 0.01s 5324KB
stdin
4
0 0 -2 2
-1 -1 4 4
4 0 2 0
6 0 8 0
1 0 1 1
-1 0 1 1
-8 9 2 6
-10 -10 17 20
stdout
1.0000000000
2.0000000000
0.0000000000
1.7839059510