As the Crow Flies

Time limit1sMemory limit128 MB

Summary
Given a network of cities with lat/lon coordinates and flight legs, find the pair whose shortest route (sum of great-circle leg distances) is the largest.
Level

Hard8 of 10

Topics
Graph, Shortest path, Geometry, Implementation
Solved
No attempts yet

Problem

As the president of a startup airline, you have launched a frequent flier program that rewards customers for every mile they travel. Because you run a for-profit company, you want to minimize the number of frequent flier miles a passenger can earn on any single trip. To estimate how many miles a customer could accumulate on your existing network, you decide to write a program.

Assumptions:

  • A passenger's itinerary is one-way (there is no return flight).
  • Every itinerary follows the shortest route from the departure city to the destination city.
  • Frequent flier miles are measured "as the crow flies," that is, along the shortest path over the earth's surface connecting the cities on the route.
  • The earth's surface is a perfect sphere with a radius of 4000 miles.

Input

The first line contains a single integer nn, the number of data sets. Each data set is formatted as follows.

A data set has three parts:

  1. Header line — a single line X Y, where XX is the number of cities and YY is the number of flight legs in the network. Both are positive integers less than 100.

  2. City list — one city per line, in the format C LA NS LO EW, where:

    • C is the city name (no spaces, alphabetic, only the first letter uppercase).
    • LA is the latitude in degrees (0 to 90).
    • NS is the latitude hemisphere (N for north or S for south of the equator).
    • LO is the longitude in degrees (0 to 180).
    • EW is the longitude hemisphere (E for east or W for west of the prime meridian).
  3. Flight list — one city pair per line, in the format B C, meaning cities B and C are directly connected by a flight leg. B C is equivalent to C B.

Notes:

  • Some longitudes can be written in more than one way (for example, 180E = 180W).
  • All latitude and longitude degrees in the input are integers.
  • The network is connected (there is at least one route between any two cities).

Output

For each data set, output the two cities that are farthest apart, where "farthest" means the pair whose shortest route between them is the longest over all city pairs. There are guaranteed to be no ties. Print the two city names on one line, separated by a single space, sorted in dictionary order.

Examples3

  1. Example 1

    Input
    2
    6 5
    Northpole 90 N 87 E
    Southpole 90 S 180 W
    Equatorone 0 N 45 W
    Equatortwo 0 S 90 E
    Equatorthree 0 S 180 E
    Equatorfour 0 N 46 W
    Equatorone Equatortwo
    Equatortwo Equatorthree
    Equatorthree Equatorfour
    Northpole Equatortwo
    Southpole Equatorthree
    2 1
    Northpole 90 N 0 E
    Southpole 90 S 0 W
    Southpole Northpole
    
    Expected output
    Equatorfour Equatorone
    Northpole Southpole
    
  2. Example 2

    Input
    1
    2 1
    Northpole 90 N 0 E
    Southpole 90 S 0 W
    Northpole Southpole
    
    Expected output
    Northpole Southpole
    
  3. Example 3

    Input
    1
    3 2
    Alpha 0 N 0 E
    Beta 0 N 90 E
    Gamma 0 N 170 E
    Alpha Beta
    Beta Gamma
    
    Expected output
    Alpha Gamma