Cod sursa(job #1533133)

Utilizator andreiiiiPopa Andrei andreiiii Data 22 noiembrie 2015 09:34:38
Problema Robot Scor 95
Compilator java Status done
Runda Arhiva de probleme Marime 13.17 kb
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream;
        try {
            inputStream = new FileInputStream("robot.in");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        OutputStream outputStream;
        try {
            outputStream = new FileOutputStream("robot.out");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        robot solver = new robot();
        solver.solve(1, in, out);
        out.close();
    }

    static class robot {
        private final double eps = 1e-10;
        private byte[][] graph;
        private boolean[] badPoint;

        private double distSquare(Point a, Point b) {
            return (a.x - b.x) * (a.x - b.x) +
                    (a.y - b.y) * (a.y - b.y);
        }

        private double dist(Point a, Point b) {
            return Math.sqrt(distSquare(a, b));
        }

        private double slope(Point a, Point b) {
            if (a.x == b.x) return 1e99;
            return (a.y - b.y) / (a.x - b.x);
        }

        private double crossProduct(Point o, Point a, Point b) {
            return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            Point[] robot = new Point[N];
            for (int i = 0; i < N; ++i) {
                robot[i] = new Point(in.nextDouble(), in.nextDouble());
            }

            int M = in.nextInt();
            Point[][] obstacles = new Point[M][];
            for (int i = 0; i < M; ++i) {
                int K = in.nextInt();
                obstacles[i] = new Point[K];
                for (int j = 0; j < K; ++j)
                    obstacles[i][j] = new Point(in.nextDouble(), in.nextDouble());
            }

            for (int i = 0; i < M; ++i)
                obstacles[i] = expand(obstacles[i], robot);

        /*if (!Arrays.deepEquals(obstacles[0], obstacles[1]))
            throw new RuntimeException();*/

            double minx = robot[0].x, miny = robot[0].y;
            for (int i = 1; i < N; ++i) {
                minx = Math.min(minx, robot[i].x);
                miny = Math.min(miny, robot[i].y);
            }

            //System.err.println(Arrays.toString(obstacles[0]));

            Point start = new Point(minx, miny), end = new Point(in.nextDouble(), in.nextDouble());
            if (!checkPoint(end, obstacles)) {
                out.println(-1);
                return;
            }

            int countPoints = 2;
            for (int i = 0; i < M; ++i)
                countPoints += obstacles[i].length;

            Point[] allPoints = new Point[countPoints];
            allPoints[0] = start;
            countPoints = 1;
            for (Point[] obstacle : obstacles)
                for (Point p : obstacle)
                    allPoints[countPoints++] = p;

            allPoints[countPoints++] = end;

            //System.err.flush();

            double ans = shortestPath(allPoints, obstacles, 0, countPoints - 1);
            out.println(ans != -1 ? String.format("%.2f", ans) : "-1");
        }

        private Point[] expand(Point[] poly1, Point[] poly2) {
            int N = poly1.length, M = poly2.length;
            double minx = poly2[0].x, miny = poly2[0].y;
            for (int i = 1; i < M; ++i) {
                minx = Math.min(minx, poly2[i].x);
                miny = Math.min(miny, poly2[i].y);
            }

            Point[] allPoints = new Point[N * M];
            for (int i = 0; i < N; ++i) {
                for (int j = 0; j < M; ++j) {
                    double nx = poly1[i].x - poly2[j].x + minx;
                    double ny = poly1[i].y - poly2[j].y + miny;
                    allPoints[M * i + j] = new Point(nx, ny);
                }
            }
            //System.err.println(Arrays.toString(allPoints));
            //System.err.flush();

            return convexHull(allPoints);
        }

        private Point[] convexHull(Point[] points) {
            int pos = 0;
            for (int i = 1; i < points.length; ++i)
                if (points[i].x < points[pos].x ||
                        (Math.abs(points[i].x - points[pos].x) < eps &&
                                points[i].x < points[pos].y)) pos = i;

            if (pos != 0) {
                Point aux = points[pos];
                points[pos] = points[0];
                points[0] = aux;
            }

            final Point ref = points[0];
            Arrays.sort(points, 1, points.length, new Comparator<Point>() {
                public int compare(Point o1, Point o2) {
                    double angle1 = slope(ref, o1), angle2 = slope(ref, o2);
                    if (Math.abs(angle1 - angle2) < eps)
                        return Double.compare(distSquare(ref, o1), distSquare(ref, o2));
                    return Double.compare(angle1, angle2);
                }
            });

            Point[] result = new Point[points.length];
            int top = 0;
            for (Point point : points) {
                while (top >= 2 && crossProduct(result[top - 2], result[top - 1], point) < eps)
                    top--;
                result[top++] = point;
            }
            return Arrays.copyOf(result, top);
        }

        private boolean checkPoint(Point point, Point[][] polygons) {
            for (Point[] poly : polygons)
                if (isInside(point, poly))
                    return false;
            return true;
        }

        private boolean isInside(Point point, Point[] polygon) {
            boolean inside = true;
            for (int i = 0; i < polygon.length; ++i) {
                int next = (i + 1) % polygon.length;
                inside &= crossProduct(polygon[i], polygon[next], point) > eps;
            }
            return inside;
        }

        private double shortestPath(Point[] points, Point[][] obstacles, int start, int end) {
            int N = points.length;
            double[] dist = new double[N];
            Arrays.fill(dist, 1e99);
            dist[start] = 0;
            TreeSet<State> orderedNodes = new TreeSet<State>();
            orderedNodes.add(new State(start, 0));

            graph = new byte[N][N];
            badPoint = new boolean[N];
            for (int i = 0; i < N; ++i)
                badPoint[i] = !checkPoint(points[i], obstacles);
            while (!orderedNodes.isEmpty()) {
                int node = orderedNodes.first().node;
                double cost = orderedNodes.pollFirst().dist;

                if (cost > dist[node]) continue;

                if (node == end) return cost;

                for (int i = 0; i < N; ++i) {
                    if (connected(points, obstacles, node, i) &&
                            dist[i] > dist[node] + dist(points[node], points[i])) {
                        dist[i] = dist[node] + dist(points[node], points[i]);
                        orderedNodes.add(new State(i, dist[i]));
                    }
                }
            }

            return -1;
        }

        private boolean connected(Point[] points, Point[][] obstacles, int x, int y) {
            if (x > y) {
                int aux = x;
                x = y;
                y = aux;
            }
            if (graph[x][y] != 0) return graph[x][y] == 1;
            if (badPoint[y]) return false;
            if (points[x].equals(points[y])) {
                graph[x][y] = 1;
                return true;
            }
            if (!checkPoint(points[y], obstacles)) {
                graph[x][y] = -1;
                return false;
            }
            for (Point[] obstacle : obstacles) {
                if (obstacle.length < 2) continue;
                if (intersectsPolygon(points[x], points[y], obstacle)) {
                    graph[x][y] = -1;
                    return false;
                }
            }

            graph[x][y] = 1;
            return true;
        }

        private boolean intersectsPolygon(Point a, Point b, Point[] polygon) {
            int countTouches = 0;
            for (int i = 0; i < polygon.length; ++i) {
                Point c = polygon[i], d = polygon[(i + 1) % polygon.length];
                if (Math.abs(crossProduct(c, d, a)) < eps &&
                        Math.abs(crossProduct(c, d, b)) < eps) return false;
                if (intersects(a, b, c, d)) return true;
                if (onSegment(c, a, b)) countTouches++;
                if (crossProduct(a, b, c) * crossProduct(a, b, d) < -eps &&
                        (onSegment(a, c, d) || onSegment(b, c, d)))
                    countTouches++;

                d = polygon[(i + 2) % polygon.length];
                if (intersects(a, b, c, d)) return true;
            }

            return countTouches > 1;
        }

        private boolean onSegment(Point o, Point a, Point b) {
            return Math.abs(crossProduct(o, a, b)) < eps &&
                    Math.min(a.x, b.x) < o.x && o.x < Math.max(a.x, b.x) &&
                    Math.min(a.y, b.y) < o.y && o.y < Math.max(a.y, b.y);
        }

        private boolean intersects(Point a, Point b, Point c, Point d) {
            return (crossProduct(c, d, a) * crossProduct(c, d, b) < -eps) &&
                    (crossProduct(a, b, c) * crossProduct(a, b, d) < -eps);
        }

        private class Point {
            double x;
            double y;

            Point(double x, double y) {
                this.x = x;
                this.y = y;
            }

            public boolean equals(Object e) {
                if (!(e instanceof Point)) return false;
                Point other = (Point) e;
                return Math.abs(x - other.x) < eps && Math.abs(y - other.y) < eps;
            }

            public String toString() {
                return String.format("{%.2f, %.2f}", x, y);
            }

        }

        private class State implements Comparable<State> {
            int node;
            double dist;

            State(int node, double dist) {
                this.node = node;
                this.dist = dist;
            }

            public int compareTo(State other) {
                if (Math.abs(dist - other.dist) < eps)
                    return Integer.compare(node, other.node);
                return Double.compare(dist, other.dist);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}