A Star, Not a Tree?
InterviewTime limit1sMemory limit128 MB
Given up to 100 points in the plane, pick one hub location that minimizes the sum of Euclidean distances to all points, and report the rounded minimum.
- Level
Medium6 of 10
- Topics
- Geometry, Math, Binary search, Implementation
- Solved
- No attempts yet
Problem
Luke wants to upgrade his home computer network from 10Mbps to 100Mbps. His old network used 10base2 (coaxial) cables, which let him connect any number of computers together in a single line, and Luke was proud that he had solved a nasty NP-complete problem to minimize the total cable length.
Unfortunately, that cabling can no longer be used. The 100Mbps system uses 100baseT (twisted-pair) cables, and each such cable connects exactly two devices: either two network cards, or one network card and a hub (an electronic device that interconnects several cables). Luke has two choices. (1) Buy network cards, put one or more cards in each computer, and chain them all together. (2) Buy network cards and one hub, and connect each computer directly to the hub. The first option requires configuring the operating system to forward network traffic, but after installing Winux 2007.2 forwarding stopped working and Luke could not re-enable it. Having never heard of Prim or Kruskal, he settled on the second option: network cards and one hub.
Luke lives in a loft, so he can run the cables and place the hub anywhere. He will not move his computers. He wants to minimize the total length of cable he must buy.
Equivalently, given the coordinates of computers in the plane, choose a single hub location that minimizes the total Euclidean distance from the hub to every computer,
and report that minimum total.
Input
The first line contains a positive integer , the number of computers. Each of the next lines gives the coordinates, in millimetres, of one computer in the room. All coordinates are integers between and .
Output
Print one number: the total length of the cable segments, rounded to the nearest millimetre.