This page is still under construction.

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

Equidistance

Time limit1sMemory limit128 MB

Summary
Given coordinates of locations on a sphere, for each query find the surface distance from a third point to the great circle equidistant from two given points.
Level

Medium5 of 10

Topics
Geometry, Math, Implementation, Simulation
Solved
No attempts yet

Problem

Alice and Bob have not met for a long time. Bob is unhappy about this and keeps urging Alice to finally make time for a meeting.

Alice: Maybe we should meet on neutral ground.

Bob: I heard the same thing from you two years ago.

Alice: I know. I just haven't found a suitable place yet that is roughly the same distance from your home and from mine.

Bob: On the surface of a sphere, the locus of points equidistant from two given points is a great circle — the one that meets the great circle through the two points at a right angle, at their midpoint. If you only need approximately equal distances, that becomes a band a few kilometres wide and about 40000 km long. Not all of it is water, so finding a fitting place is perfectly feasible.

Alice: If I let you pick anything, we'll certainly end up in Honolulu.

Bob: Which is not a bad idea. So, may I pick anything?

Alice: As long as I don't have to accept it — but I'm open to suggestions.

Bob: Honolulu?

Alice: Is it even on the locus you just described?

Bob: Not quite…

Now to the facts. Given two locations on Earth's surface, consider the locus of all points equidistant from both — a great circle. For a third given location, compute its surface distance to this locus. Assume Earth is a sphere with a radius of 6378 km.

Input

The input consists of two parts: a list of locations followed by a list of queries.

The location list has up to 100 lines, one location per line. Each line contains a name and two floating-point numbers separated by whitespace: the location's name, its latitude, and its longitude. Names are unique, shorter than 30 characters, and contain no whitespace. Latitudes are between -90 (South Pole) and 90 (North Pole) inclusive. Longitudes are between -180 and 180 inclusive, where negative values lie west of the prime meridian and positive values lie east of it (the prime meridian passes through Greenwich, London). The location list ends with a line containing a single #.

Each line of the query list contains three location names. The first is Alice's home, the second is Bob's home, and the third is a candidate meeting point. The query list ends with a line containing a single #.

Output

For each query, print one line:

M is x km off A/B equidistance.

Replace M with the meeting point's name, A and B with the two home names, and x with the computed surface distance rounded to the nearest integer.

If any of the three locations named in a query does not appear in the location list, print ? in place of the distance.

Examples2

  1. Example 1

    Input
    Ulm             48.700 10.500
    Freiburg        47.700 9.500
    Philadelphia    39.883 -75.250
    SanJose         37.366 -121.933
    Atlanta         33     -84
    Eindhoven       52     6
    Orlando         28     -82
    Vancouver       49     -123
    Honolulu        22     -157
    NorthPole       90     0
    SouthPole       -90    0
    #
    Ulm Freiburg Philadelphia
    SanJose Atlanta Eindhoven
    Orlando Vancouver Honolulu
    NorthPole SouthPole NorthPole
    Ulm SanDiego Orlando
    NorthPole SouthPole SouthPole
    Ulm Honolulu SouthPole
    #
    
    Expected output
    Philadelphia is 690 km off Ulm/Freiburg equidistance.
    Eindhoven is 3117 km off SanJose/Atlanta equidistance.
    Honolulu is 4251 km off Orlando/Vancouver equidistance.
    NorthPole is 10019 km off NorthPole/SouthPole equidistance.
    Orlando is ? km off Ulm/SanDiego equidistance.
    SouthPole is 10019 km off NorthPole/SouthPole equidistance.
    SouthPole is 1494 km off Ulm/Honolulu equidistance.
    
  2. Example 2

    Input
    NorthPole 90 0
    SouthPole -90 0
    Equator 0 0
    London 51.5 0
    #
    NorthPole SouthPole Equator
    NorthPole SouthPole London
    #
    
    Expected output
    Equator is 0 km off NorthPole/SouthPole equidistance.
    London is 5733 km off NorthPole/SouthPole equidistance.