Messenger in the Seven Kingdoms
InterviewTime limit2sMemory limit1024 MB
For each starting city, the messenger always moves to the nearest unvisited city, and the task asks for the smallest total squared distance over all starts.
- Level
Easy3 of 10
- Topics
- Simulation, Brute force
- Solved
- No attempts yet
Statement
Once again the Seven Kingdoms have trouble. King Robert Baratheon has heard rumors of dragons, and he wants to warn all cities in the kingdom as soon as possible. Unfortunately, Joffrey, the king's son, shot almost all the carrier pigeons with a slingshot. Only one is left. The king wants to pick one city to send the messenger from, so that the messenger reaches all the other cities in the minimum time.
The messengers of the Seven Kingdoms are very lazy. They do not plan a route through the cities. They follow a fixed strategy instead:
Suppose the messenger is in city , and is the set of cities he has already visited. His next stop is city , the city closest to that is not in . If several cities are equally close, he goes to the one with the smallest number.
The cities are points on a plane with Cartesian coordinates . The time the messenger needs to travel between two cities is the squared Euclidean distance between their points.
Help King Robert choose the city from which the messenger visits all other cities in the minimum total time.
Input
The first line contains the number of cities (). Each of the next lines contains the coordinates and of one city ().
Output
Print one line with the minimum time needed to notify all the cities.