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