Shortest Path Problem (Nanyang oj7) (policy problem)

Source: Internet
Author: User

Shortest Path Problem (Nanyang oj7) (policy problem)
Block shortest path problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 4

Description
There are many residents in a block. The street in the block can only be east-west, north-south.

Residents can only walk along the street.

The intervals between different streets are equal.

(X, y) indicates the block where the residents are sitting.

For example, (4th) indicates that the user has 20th streets in the east-west direction and streets in the North-South direction.

Now we need to build a post office so that the total distance from each household to the Post Office is the least.

The post office should be built in that place to minimize the total distance of all residents;

Input
The first line is an integer of n <20, indicating that there are n groups of test data. below is n groups of data;
The first row of each group is an integer m <20, indicating that the Group has m households. each row of the following m rows has two integers 0. M rows are followed by a new set of data;
Output
The minimum distance from each group of data output to the Post Office is the sum. Press enter to finish;
Sample Input
231 12 11 252 9 5 2011 91 11 20
Sample output
244
Source
Typical questions
Uploaded

Iphxer

Refer to others' ideas for AC:

First, find the median of the horizontal and vertical coordinates (that is, the post office position), and then because the street only has two directions: horizontal and vertical,

Therefore, the shortest distance is the horizontal and vertical coordinates of each position and the absolute value of the Post Office. The code below is easy to simplify.

#include
     
      #includeusing namespace std;int main(){int a[22],b[22];int test,i,n,sum;scanf("%d",&test);while(test--){scanf("%d",&n);for(i=0;i
      
       

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.