Algorithm


D. Barcelonian Distance
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In this problem we consider a very simplified model of Barcelona city.

Barcelona can be represented as a plane with streets of kind x=c�=� and y=c�=� for every integer c (that is, the rectangular grid). However, there is a detail which makes Barcelona different from Manhattan. There is an avenue called Avinguda Diagonal which can be represented as a the set of points (x,y)(�,�) for which ax+by+c=0��+��+�=0.

One can walk along streets, including the avenue. You are given two integer points A and B somewhere in Barcelona. Find the minimal possible distance one needs to travel to get to B from A.

Input

The first line contains three integers ab and c (109a,b,c109−109≤�,�,�≤109, at least one of a and b is not zero) representing the Diagonal Avenue.

The next line contains four integers x1�1y1�1x2�2 and y2�2 (109x1,y1,x2,y2109−109≤�1,�1,�2,�2≤109) denoting the points A=(x1,y1)�=(�1,�1) and B=(x2,y2)�=(�2,�2).

Output

Find the minimum possible travel distance between A and B. Your answer is considered correct if its absolute or relative error does not exceed 10610−6.

Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if |ab|max(1,|b|)106|�−�|max(1,|�|)≤10−6.

Examples
input
Copy
1 1 -3
0 3 3 0
output
Copy
4.2426406871
input
Copy
3 1 -9
0 3 3 -1
output
Copy
6.1622776602
Note

The first example is shown on the left picture while the second example us shown on the right picture below. The avenue is shown with blue, the origin is shown with the black dot.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

struct line {
  line() {}
  line(double a, double b, double c) : a(a), b(b), c(c) {}
  double a, b, c;

  void print() {
    printf("Line %lf %lf %lf\n", a, b, c);
  }
};

struct point {
  point() {}
  point(double x, double y) : x(x), y(y) {}
  double x, y;

  bool ok() {
    return x != FLT_MAX && y != FLT_MAX;
  }

  void print() {
    printf("%lf %lf\n", x, y);
  }
};

double dist(point a, point b) {
  return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}

line pointsToLine(point p1, point p2) {
  if(fabs(p1.x - p2.x) < 1e-15)
    return line(1.0, 0.0, -p1.x);
  else {
    line l;
    l.a = -(double)(p1.y - p2.y) / (p1.x - p2.x);
    l.b = 1.0;
    l.c = -(double)(l.a * p1.x) - p1.y;
    return l;
  }
}

point lineLineIntersection(line l1, line l2) {
  double det = l1.a * l2.b - l2.a * l1.b;

  if(det == 0)
    return point(FLT_MAX, FLT_MAX);
  else {
    double x = (l1.b * l2.c - l2.b * l1.c) / det;
    double y = (l2.a * l1.c - l1.a * l2.c) / det;
    return point(x, y);
  }
}

line l;
point p, q;

int main() {
  scanf("%lf %lf %lf", &l.a, &l.b, &l.c);
  scanf("%lf %lf %lf %lf", &p.x, &p.y, &q.x, &q.y);

  line lp1 = pointsToLine(p, point(p.x, p.y + 1.0));
  line lp2 = pointsToLine(p, point(p.x + 1.0, p.y));

  line lq1 = pointsToLine(q, point(q.x, q.y + 1.0));
  line lq2 = pointsToLine(q, point(q.x + 1.0, q.y));

  point ps1 = lineLineIntersection(l, lp1);
  point ps2 = lineLineIntersection(l, lp2);

  point qs1 = lineLineIntersection(l, lq1);
  point qs2 = lineLineIntersection(l, lq2);

  double res = 2e18;

  if(ps1.ok() && qs1.ok())
    res = min(res, dist(p, ps1) + dist(ps1, qs1) + dist(qs1, q));

  if(ps1.ok() && qs2.ok())
    res = min(res, dist(p, ps1) + dist(ps1, qs2) + dist(qs2, q));

  if(ps2.ok() && qs1.ok())
    res = min(res, dist(p, ps2) + dist(ps2, qs1) + dist(qs1, q));

  if(ps2.ok() && qs2.ok())
    res = min(res, dist(p, ps2) + dist(ps2, qs2) + dist(qs2, q));

  printf("%.10lf\n", min(res, fabs(p.x - q.x) + fabs(p.y - q.y)));

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 1 -3
0 3 3 0

Output

x
+
cmd
4.2426406871
Advertisements

Demonstration


Codeforces Solution-D. Barcelonian Distance-Solution in C, C++, Java, Python,Barcelonian Distance,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+