ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

The minimal circle covering set of points...
Posted by Rumter (2) 26 Jul 2008 16:29
The set of points is given, it is necessary to find the circle of the minimal radius covering them.

Please, help me. My algo is O(n^4) but it very slow.
Re: The minimal circle covering set of points...
Posted by Rumter (2) 27 Jul 2008 15:56
void min_disk3 (point a, point b, point c, point &circ, double &r)
{
    if (on_line (a, b, c))
    {
        point p1 = minp (minp (a, b), c);
        point p2 = maxp (maxp (a, b), c);

        circ = part (p1, p2, 1, 1);
        r = 0.5 * dist (p1, p2);

        return;
    }

    point mab = part (a, b, 1, 1);
    point mac = part (a, c, 1, 1);
    double a1 = a.x - b.x;
    double b1 = a.y - b.y;
    double c1 = - a1 * mab.x - b1 * mab.y;
    double a2 = a.x - c.x;
    double b2 = a.y - c.y;
    double c2 = - a2 * mac.x - b2 * mac.y;

    double x, y;
    x = (b2 * c1 - b1 * c2) / (a2 * b1 - a1 * b2);
    if (!equald (b1, eps))
        y = - (a1 * x + c1) / b1;
    else
        y = - (a2 * x + c2) / b2;

    circ = point (x, y);
    r = dist (circ, a);
}

void min_disk2 (point p[], int n, point a, point b, point &circ, double &r)
{
    circ = part (a, b, 1, 1);
    r = dist (circ, a);
    for (int i = 0; i < n; ++ i)
        if (lessd (r, dist (p[i], circ)))
            min_disk3 (p[i], a, b, circ, r);
}
void min_disk1 (point p[], int n, point a, point &circ, double &r)
{
    circ = part (p[0], a, 1, 1);
    r = dist (circ, a);
    for (int i = 1; i < n; ++ i)
        if (lessd (r, dist (p[i], circ)))
            min_disk2 (p, i, a, p[i], circ, r);
}
void min_disk (point p[], int n, point &circ, double &r)
{
    circ = part (p[0], p[1], 1, 1);
    r = dist (circ, p[0]);
    for (int i = 2; i < n; ++ i)
        if (lessd (r, dist (p[i], circ)))
            min_disk1 (p, i, p[i], circ, r);
}
Re: The minimal circle covering set of points...
Posted by Rumter (2) 27 Jul 2008 15:57
O(nlog3n) :-)