Mathematical and Physical Journal
for High Schools
Issued by the MATFUND Foundation
 Already signed up? New to KöMaL?

# Problem S. 59. (January 2011)

S. 59. An investor plans to build a new luxury supermarket in the heart Manhattan. He knows that the building should be built as close to his future clients as possible. He already accurately surveyed the number of possible clients from each part of the city. Your task is to specify a location for the supermarket such that customers can reach it with the least travelling on the average.

Your program should solve this task. As usual, the map of Manhattan is considered as a regular grid with two-way streets, and crossings having integer coordinates.

For simplicity, we identify clients' flats and the supermarket with the corresponding nearest crossings. Instead of the Euclidean distance, we measure the distance between crossing with coordinates (xi,yi) and crossing with coordinates (xj,yj) by the following formula (which is often termed as Manhattan distance): , measuring the shortest distance along the grid.

The result of the investor's survey is read from the standard input: its first line contains the number of crossings , and the following N lines describe each crossing. There are three integers in the (i+1)st line separated by a space: the two coordinates of the ith crossing and the number of expected clients 1wi1000 living there.

Your program should find the optimal crossing such that the average weighted distance between and all the crossings listed above is minimal (with weights being the number of clients living on the actual crossing). In other words, the quantity

is to be minimized. The standard output should only contain three numbers separated by a space: the two coordinates x0, y0 of the optimal location and the average weighted distance with 4 digits of precision. If there are many solutions, any of them can be given. In the example, Példa bemenet'' is a sample input, kimenet'' is output, while Térképvázlat'' is the corresponding map.

Evaluation: the maximal 8 points can be awarded if your program solves the largest test cases within one minute. 5 points can be awarded if your program correctly handles test cases already with N1000. The documentation is worth 2 points.

The source code and project files of your solution -- without the .exe or any other auxiliary files generated by the compiler -- should be submitted together with a short documentation (s59.txt, s59.pdf, ...) in a compressed folder s59.zip.

(10 pont)

Deadline expired on February 10, 2011.

Sorry, the solution is available only in Hungarian. Google translation

Megoldásvázlat

A megoldáshoz azt kellett észrevenni, hogy az átlagos távolság épp akkor minimális, amikor a súlyozott távolságösszeg minimális (hisz egymás konstansszorosai), ennek pedig függőleges és vízszintes összetevői egymástól teljesen függetlenek, így külön-külön minimalizálhatóak.

Tehát két egydimenziós problémát kaptunk: két, abszolútérték-függvények összegeiből felépített függvény minimumhelyét kell meghatároznunk. Az ilyen függvények konvexek, és egyenes szakaszokra bonthatóak: minimumhelyük tehát ott van, ahol ezen szakaszok meredeksége negatívból pozitívba fordul, amely egy rendezés után lineáris időben meghatározható.

### Statistics:

 8 students sent a solution. 10 points: Borsos 607 Zalán, Nagy 111 Miklós, Szabó 928 Attila. 9 points: Nagy Róbert. 8 points: 1 student. 7 points: 1 student. 5 points: 1 student. 4 points: 1 student.

Problems in Information Technology of KöMaL, January 2011