けんちょんの競プロ精進記録

競プロの精進記録や小ネタを書いていきます

AOJ 2385 Shelter (JAG 冬合宿 2010 day4-G) (700 点)

ボロノイ図の応用問題!

問題概要

頂点数  M の凸多角形があって、その内部に  N 個の点が与えられる。凸多角形の内部に一様ランダムに点を打つとき、以下の値の期待値を求めよ。

  • 「その点  x から  N 個の点までの距離の最小値」を二乗した値

制約

  •  M, N \le 100
  • 各座標値の絶対値  \le 1000

考えたこと

まずはボロノイ図を考えることで、 N 個の点それぞれについて、その点と最も距離が近い点の領域を抽出することができる。

よって、「1 点と凸多角形」に関する問題へと帰着される (最後にボロノイ図の各領域の面積で重みをつけた平均値をとればよい)。

さらにこれは、「1 点と、その 1 点を頂点の一つにもつ三角形」に関する問題へと帰着できる。すなわち、1 点と、凸多角形の各辺のなす各三角形について考えていけばよい。

三角形 PAB

次の問題が解ければ OK。


3 点 P, A, B の座標が与えられる。三角形 PAB の内部に一様ランダムに点を打ったとき、その点と点 P との距離の二乗の期待値を求めよ。


この問題を解くために

  • 点 P から辺 AB に下ろした垂線の足を H とし、PH の長さを  h とする
  • 点 P を原点にとり、点 H の座標が  (h, 0) となるよにする
  • さらに、A の座標を  (h, a)、B の座標を  (h, b) とする
    •  a \le b を仮定する

このとき、三角形 PAB の内部の点は

  •  0 \le x \le h
  •  \frac{ax}{h} \le y \le \frac{bx}{h}

の範囲を動くといえる。よってこの範囲で  x^{2} + y^{2} を積分して、三角形 PAB の面積  \frac{1}{2} h(b-a) で割ればよい。計算するとこの値は、

 \frac{1}{6}(a^{2} + ab + b^{2} + 3h^{2})

となる。

#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;

using DD = double;
const DD INF = 1LL<<60;      // to be set appropriately
const DD EPS = 1e-6;        // to be set appropriately
const DD PI = acosl(-1.0);
DD torad(int deg) {return (DD)(deg) * PI / 180;}
DD todeg(DD ang) {return ang * 180 / PI;}

/* Point */
struct Point {
    DD x, y;
    Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {}
    friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << ", " << p.y << ')';}
};
inline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);}
inline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);}
inline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);}
inline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);}
inline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);}
inline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);}
inline Point conj(const Point &p) {return Point(p.x, -p.y);}
inline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}
inline Point rot90(const Point &p) {return Point(-p.y, p.x);}
inline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;}
inline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;}
inline DD norm(const Point &p) {return dot(p, p);}
inline DD abs(const Point &p) {return sqrt(dot(p, p));}
inline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;}
inline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;}
inline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);}
inline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);}
inline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);}

/* Line */
struct Line : vector<Point> {
    Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) {
        this->push_back(a);
        this->push_back(b);
    }
    friend ostream& operator << (ostream &s, const Line &l) {return s << '{' << l[0] << ", " << l[1] << '}';}
};

/* Circle */
struct Circle : Point {
    DD r;
    Circle(Point p = Point(0.0, 0.0), DD r = 0.0) : Point(p), r(r) {}
    friend ostream& operator << (ostream &s, const Circle &c) {return s << '(' << c.x << ", " << c.y << ", " << c.r << ')';}
};


///////////////////////
// 多角形
///////////////////////

// 多角形の面積
DD calc_area(const vector<Point> &pol) {
    DD res = 0.0;
    for (int i = 0; i < pol.size(); ++i) {
        res += cross(pol[i], pol[(i+1)%pol.size()]);
    }
    return res/2.0L;
}

// convex cut
int ccw_for_convexcut(const Point &a, const Point &b, const Point &c) {
    if (cross(b-a, c-a) > EPS) return 1;
    if (cross(b-a, c-a) < -EPS) return -1;
    if (dot(b-a, c-a) < -EPS) return 2;
    if (norm(b-a) < norm(c-a) - EPS) return -2;
    return 0;
}
vector<Point> crosspoint_for_convexcut(const Line &l, const Line &m) {
    vector<Point> res;
    DD d = cross(m[1] - m[0], l[1] - l[0]);
    if (abs(d) < EPS) return vector<Point>();
    res.push_back(l[0] + (l[1] - l[0]) * cross(m[1] - m[0], m[1] - l[0]) / d);
    return res;
}
vector<Point> convex_cut(const vector<Point> &pol, const Line &l) {
    vector<Point> res;
    for (int i = 0; i < pol.size(); ++i) {
        Point p = pol[i], q = pol[(i+1)%pol.size()];
        if (ccw_for_convexcut(l[0], l[1], p) != -1) {
            if (res.size() == 0) res.push_back(p);
            else if (!eq(p, res[res.size()-1])) res.push_back(p);
        }
        if (ccw_for_convexcut(l[0], l[1], p) * ccw_for_convexcut(l[0], l[1], q) < 0) {
            vector<Point> temp = crosspoint_for_convexcut(Line(p, q), l);
            if (temp.size() == 0) continue;
            else if (res.size() == 0) res.push_back(temp[0]);
            else if (!eq(temp[0], res[res.size()-1])) res.push_back(temp[0]);
        }
    }
    return res;
}

// Voronoi-diagram
// pol: outer polygon, ps: points
// find the polygon nearest to ps[ind]
Line bisector(const Point &p, const Point &q) {
    Point c = (p + q) / 2.0L;
    Point v = (q - p) * Point(0.0L, 1.0L);
    v = v / abs(v);
    return Line(c - v, c + v);
}

vector<Point> voronoi(const vector<Point> &pol, const vector<Point> &ps, int ind) {
    vector<Point> res = pol;
    for (int i = 0; i < ps.size(); ++i) {
        if (i == ind) continue;
        Line l = bisector(ps[ind], ps[i]);
        res = convex_cut(res, l);
    }
    return res;
}

// 1:a-bから見てcは左側(反時計回り)、-1:a-bから見てcは右側(時計回り)
// 2:c-a-bの順に一直線上、-2:a-b-cの順に一直線上、0:a-c-bの順に一直線上
int ccw(const Point &a, const Point &b, const Point &c) {
    if (cross(b-a, c-a) > EPS) return 1;
    if (cross(b-a, c-a) < -EPS) return -1;
    if (dot(b-a, c-a) < -EPS) return 2;
    if (norm(b-a) < norm(c-a) - EPS) return -2;
    return 0;
}

Point proj(const Point &p, const Line &l) {
    DD t = dot(p - l[0], l[1] - l[0]) / norm(l[1] - l[0]);
    return l[0] + (l[1] - l[0]) * t;
}


// 三角形 PAB の内部の点の、P との距離の二乗の期待値
DD solve(const Point &P, const Point &A, const Point &B) {
    auto H = proj(P, Line(A, B));
    DD h = abs(H - P), a = abs(H - A), b = abs(H - B);
    if (ccw(A, H, B) == -2) a = -a;
    return (a*a + a*b + b*b + h*h*3) / 6;
}

int main() {
    int N, M;
    while (cin >> M >> N) {
        vector<Point> pol(M), ps(N);
        for (int i = 0; i < M; ++i) cin >> pol[i].x >> pol[i].y;
        for (int i = 0; i < N; ++i) cin >> ps[i].x >> ps[i].y;
        DD res = 0;
        DD allarea = 0;
        for (int i = 0; i < N; ++i) {
            auto vpol = voronoi(pol, ps, i);
            for (int j = 0; j < vpol.size(); ++j) {
                auto P = ps[i], A = vpol[j], B = vpol[(j+1)%vpol.size()];
                vector<Point> tri({P, A, B});
                auto area = calc_area(tri);
                auto d = solve(P, A, B);
                res += d * area;
                allarea += area;
            }
        }
        cout << fixed << setprecision(10) << res/allarea << endl;
    }
}