As the Crow Flies
Time limit1sMemory limit128 MB
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 , the number of data sets. Each data set is formatted as follows.
A data set has three parts:
-
Header line — a single line
X Y, where is the number of cities and is the number of flight legs in the network. Both are positive integers less than 100. -
City list — one city per line, in the format
C LA NS LO EW, where:Cis the city name (no spaces, alphabetic, only the first letter uppercase).LAis the latitude in degrees (0 to 90).NSis the latitude hemisphere (Nfor north orSfor south of the equator).LOis the longitude in degrees (0 to 180).EWis the longitude hemisphere (Efor east orWfor west of the prime meridian).
-
Flight list — one city pair per line, in the format
B C, meaning citiesBandCare directly connected by a flight leg.B Cis equivalent toC 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.