Block Shortest path problem time limit:MS | Memory limit:65535 KB Difficulty:4
-
Describe
-
There are many residents in a block, and the streets of the block are only for the east and South directions.
Residents can only walk along the street.
The intervals between the streets are equal.
Use (x, y) to indicate the neighborhood in which the occupants sit.
For example (4,20), represents a user in east-west to 4th Street, North-south direction 20th Street.
Now to build a post office, so that the number of residents to the post office and the minimum distance.
Now this post office should be built in that place to minimize the distance of all households;
-
-
Input
-
The
-
first line is an integer n<20, which indicates that there are N groups of test data, and the following are N groups of data;
The first line of each group is an integer m<20, indicating that the group has m households, and the following m rows have two integer 0<x,y<100 per row, indicating the coordinates of a user's block.
After M line is a new set of data;
-
-
Output
-
-
each set of data output to the Post Office minimum distance and, enter the end;
-
-
Sample input
-
-
231 12 11 252 9 5 2011 91 11 20
-
-
Sample output
-
244
The post office is not necessarily in the settlement, this should pay attention to, at first did not understand good test instructions, think is coordinates point of distance ....
Code:
1#include <stdio.h>2#include <stdlib.h>3 Const intmaxn= -;4 intcmpConst void*a,Const void*b) {5 if(*(int*) a<* (int*) b)return-1;6 Else return 1;7 }8 intX[MAXN],Y[MAXN];9 intMain () {Ten intT,n; Onescanf"%d",&T); A while(t--){ -scanf"%d",&n); - for(intI=0; i<n;i++) thescanf"%d%d", x+i,y+i); -Qsort (X,n,sizeof(x[0]), CMP); -Qsort (Y,n,sizeof(y[0]), CMP);//at this point the Post Office establishment should be X[N/2],Y[N/2]; - intsum=0; + for(intI=0; i<n/2; i++){ -sum+=x[n-1-i]-x[i]+y[n-1-i]-y[i];//X[i] and x[n-1-i] to X[N/2] distance and the equivalent of x[i] to X[n-1-i] point, draw a range to see + //sum shall be added to the distance of all points to X[N/2]; A } atprintf"%d\n", sum); - } - return 0; -}
Nyoj 7-Block Shortest path problem (Manhattan distance)