Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2083
Surface:
Shortest distance from the simple versionTime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 13366 Accepted Submission (s): 5926
Problem description Winter vacation, Acboy to visit a lot of friends, it happened that all his friends home in the coordinate plane of the x-axis. Acboy can choose a friend's home to start visiting, but after each visit he must go back to the starting point before he can visit his next friend.
For example, there are 4 friends, the corresponding x-axis coordinates are 1, 2, 3, 4. When Acboy chooses a point with coordinates 2 as the starting point, the time he eventually needs is |1-2|+|2-2|+|3-2|+|4-2| = 4.
Now give the coordinates of n friends, then how should acboy go to spend the least time?
Input inputs are first a positive integer m, representing m test instances. Each instance has an input of 2 rows, first a positive integer n (n <= 500), a number of n friends, and the next line is n positive integers, representing the exact coordinates (all data is <=10000).
Output for each test instance, print the minimum time spent accessing all your friends, with one row for each instance output.
Sample Input
22 2 4 3 2 4 6
Sample Output
24
SOURCE2006/1/15 ACM Program Design Final Exam
Solving:
Note to start from his friend's house, and only to calculate the time, give the data a bit of a pit.
Code:
HDU 2083 Simple version of the shortest distance