HDU 2083 Simple version of the shortest distance

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.