Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. //no brain
  2. #include <iostream>
  3. #include <ctime>
  4. #include <iomanip>
  5. #include <vector>
  6. #include <map>
  7. #include <algorithm>
  8. #include <string>
  9. #include <cmath>
  10. #include <set>
  11. #include <unordered_set>
  12. #include <unordered_map>
  13. #include <chrono>
  14. #include <stack>
  15. #include <cassert>
  16. #include <queue>
  17. #include <deque>
  18. #include <climits>
  19. #include <cstring>
  20. #include <random>
  21. #include <bitset>
  22. #include <functional>
  23.  
  24. using namespace std;
  25.  
  26. typedef long double ld;
  27.  
  28. const ld inf = 1e9;
  29. const ld eps = 1e-9;
  30.  
  31. struct Point {
  32. ld x;
  33. ld y;
  34.  
  35. Point() {
  36. x = -1;
  37. y = -1;
  38. }
  39.  
  40. Point(ld x_, ld y_) {
  41. x = x_;
  42. y = y_;
  43. }
  44.  
  45. friend istream & operator >> (istream & in, Point &p) {
  46. in >> p.x >> p.y;
  47. return in;
  48. }
  49.  
  50. friend ostream & operator << (ostream & out, Point &p) {
  51. out << fixed << setprecision(16) << " " << p.x << " " << p.y << "\n";
  52. return out;
  53. }
  54.  
  55. ld dist(const Point &other) const{
  56. return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
  57. }
  58.  
  59. bool operator != (const Point &other) {
  60. return x != other.x || y != other.y;
  61. }
  62. };
  63.  
  64. struct Vector {
  65. ld x;
  66. ld y;
  67.  
  68. Vector(Point &a, Point &b) {
  69. x = a.x - b.x;
  70. y = a.y - b.y;
  71. }
  72.  
  73. Vector(ld x_, ld y_) {
  74. x = x_;
  75. y = y_;
  76. }
  77.  
  78. ld operator % (const Vector &other) const{
  79. return x * other.x + y * other.y;
  80. }
  81.  
  82. ld operator * (const Vector &other) const{
  83. return x * other.y - other.x * y;
  84. }
  85.  
  86. Vector operator / (ld k) const{
  87. return Vector(x / k, y / k);
  88. }
  89.  
  90. Vector operator * (ld k) const{
  91. return Vector(x * k, y * k);
  92. }
  93. };
  94.  
  95. Point operator + (const Point &tmp, const Vector &other) {
  96. return Point(tmp.x + other.x, tmp.y + other.y);
  97. }
  98.  
  99. Point intersection(Point a, Point b, Point c, Point d) {
  100. Vector v(b, a);
  101. ld s1 = Vector(c, a) * Vector(d, a);
  102. ld s2 = Vector(d, b) * Vector(c, b);
  103. ld s = s1 + s2;
  104. if (fabs(s) < eps) {
  105. return Point(-inf, -inf);
  106. }
  107. v = v / s;
  108. v = v * s1;
  109. Point O = a + v;
  110. return O;
  111. }
  112.  
  113.  
  114. signed main() {
  115. ios_base::sync_with_stdio(false);
  116. cin.tie(nullptr);
  117.  
  118. #ifdef LOCAL
  119. assert(freopen("input.txt", "r", stdin));
  120. assert(freopen("output.txt", "w", stdout));
  121. #endif
  122.  
  123. int a, b, c;
  124. cin >> a >> b >> c;
  125. int x1, y1, x2, y2;
  126. cin >> x1 >> y1 >> x2 >> y2;
  127. Point ff;
  128. Point ss;
  129. if (b == 0 || a == 0) {
  130. cout << abs(x1 - x2) + abs(y1 - y2);
  131. return 0;
  132. } else {
  133. ff = Point(0, -c / b);
  134. ss = Point(1, -(c + a) / b);
  135. }
  136.  
  137. ld ans = abs(x1 - x2) + abs(y1 - y2);
  138. for (int i = 0; i < 2; i++) {
  139. for (int j = 0; j < 2; j++) {
  140. Point f;
  141. Point s;
  142. if (i) {
  143. f = intersection(ff, ss, Point(x1, y1), Point(x1, y1 + 1));
  144. } else {
  145. f = intersection(ff, ss, Point(x1, y1), Point(x1 + 1, y1));
  146. }
  147. if (j) {
  148. s = intersection(ff, ss, Point(x2, y2), Point(x2, y2 + 1));
  149. } else {
  150. s = intersection(ff, ss, Point(x2, y2), Point(x2 + 1, y2));
  151. }
  152. ans = min(ans, f.dist(Point(x1, y1)) + s.dist(Point(x2, y2)) + f.dist(s));
  153.  
  154. }
  155. }
  156. cout << fixed << setprecision(16) << ans;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement