This page is still under construction.

Parts of this page are still being built. What you see may change.

Hiking in the Hills

Time limit2sMemory limit256 MB

Summary
Find a route from camp A to lookout B across the triangulated landscape whose highest point is as low as possible.
Level

Medium7 of 10

Topics
Union-find, Minimum spanning tree, Geometry, Graph
Solved
No attempts yet

Problem

Helen is hiking with her friends in a highland. The plan is to walk from their camp A to a lookout B.

Helen has started to feel dizzy from altitude sickness. Help the group find a route whose highest point is as low as possible.

The landscape is given as a set of triangles. A route is a polyline from A to B in which every segment lies entirely inside one of those triangles, so the route never leaves the surface. The topmost height of a route is the largest zz coordinate of any point on it. Find the smallest topmost height over all routes from A to B.

Input

The landscape covers a square region of size 106×10610^6 \times 10^6.

The first line contains one integer nn, the number of triangles in the landscape (2≤n≤20002 \le n \le 2000).

Each of the next nn lines contains nine integers xi1x_{i1}, yi1y_{i1}, zi1z_{i1}, xi2x_{i2}, yi2y_{i2}, zi2z_{i2}, xi3x_{i3}, yi3y_{i3}, zi3z_{i3}, the coordinates of one triangle. Every coordinate belongs to the closed interval [0,106][0, 10^6].

The last two lines contain three integers each: xAx_A, yAy_A, zAz_A and xBx_B, yBy_B, zBz_B, the coordinates of camp A and lookout B.

The triangles describe one consistent continuous landscape. Their projections onto the XY plane are non-degenerate and fill the square without overlapping. A vertex of one triangle never lies inside an edge of another triangle. A and B lie on the landscape surface and are different points.

Output

Print one integer, the smallest topmost height that a route from A to B can have.

Examples3

  1. Example 1

    Input
    8
    1000000 0 0 1000000 1000000 150000 600000 600000 400000
    0 1000000 0 600000 600000 400000 600000 1000000 300000
    0 1000000 0 400000 300000 150000 600000 600000 400000
    400000 0 200000 1000000 0 0 400000 300000 150000
    400000 300000 150000 1000000 0 0 600000 600000 400000
    600000 600000 400000 1000000 1000000 150000 600000 1000000 300000
    0 0 0 400000 0 200000 400000 300000 150000
    0 1000000 0 0 0 0 400000 300000 150000
    100000 700000 37500
    900000 400000 137500
    
    Expected output
    150000
    
  2. Example 2

    Input
    2
    0 0 800 1000000 0 0 1000000 1000000 900
    0 0 800 1000000 1000000 900 0 1000000 0
    900000 100000 170
    100000 900000 170
    
    Expected output
    800
    
  3. Example 3

    Input
    8
    0 0 0 500000 0 900 500000 500000 300
    0 0 0 500000 500000 300 0 500000 0
    0 500000 0 500000 500000 300 500000 1000000 900
    0 500000 0 500000 1000000 900 0 1000000 0
    500000 0 900 1000000 0 0 1000000 500000 0
    500000 0 900 1000000 500000 0 500000 500000 300
    500000 500000 300 1000000 500000 0 1000000 1000000 0
    500000 500000 300 1000000 1000000 0 500000 1000000 900
    0 0 0
    1000000 1000000 0
    
    Expected output
    300