The shortest path of a block

Source: Internet
Author: User
Tags new set

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
Source
Classic Topics
Uploaded by
Iphxer
//c++
#include <iostream> #include <algorithm>using namespace Std;int x[30],y[30],n,m,i; int main () {cin>>n;while (n--) {cin>>m;for (i=0;i<m;i++) cin>>x[i]>>y[i];sort (x,x+m); sort (y,y+m); int sum=0;for (i=0;i<m/2;i++) Sum+=x[m-1-i]-x[i]+y[m-1-i]-y[i];cout<<sum<<endl;} return 0;}        
//v1.0 C
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int n,m;
scanf ("%d", &n);
GetChar ();
While (n--) {
scanf ("%d", &m);
GetChar ();
int arr[m][2];
int sum = 0;

//input M
for (int i = 0; i < m; ++i) {
For (int j = 0; j < 2; ++j) {
scanf ("%d", &arr[i][j]);
            }
        }


int x=0;
//the bound of X
for (int i = 0; i < m; ++i) {
if (arr[i][0] > x) {
x = arr[i][0];
            }
        }


//the bound of Y
int y=0;
for (int i = 0; i < m; ++i) {
if (arr[i][1] > y) {
y = arr[i][1];
            }
        }



//min Xsum & ysum
int tmp_xmin=2000;
int tmp_ymin=2000;
int p = 0;
int q=0;
While (p<=x) {
int sumx = 0;
for (int i = 0; i < m; ++i) {
sumx + = ABS (arr[i][0]-p);
            }

if (Tmp_xmin > Sumx) {
tmp_xmin = sumx;
            }
p++;
        }

While (q<=y) {
int sumy = 0;
//y
for (int i = 0; i < m; ++i) {
Sumy + = ABS (ARR[I][1]-Q);
            }

if (Tmp_ymin > Sumy) {
tmp_ymin = Sumy;
            }
q++;
        }

sum = tmp_xmin + tmp_ymin;
printf ("%d\n", sum);
    }
return 0;
}

v0.01 C

#include <stdio.h>
#include <stdlib.h>

int main ()
{
int n,m;
scanf ("%d", &n);
GetChar ();
While (n--) {
scanf ("%d", &m);
GetChar ();
int arr[m][2];
int sum = 0;

//input M
for (int i = 0; i < m; ++i) {
For (int j = 0; j < 2; ++j) {
scanf ("%d", &arr[i][j]);
//printf ("\ n");
            }
        }

//output tests
      /*
      *
* for (int i = 0; i < m; ++i) {
For (int j = 0; j < 2; ++j) {
printf ("%d", arr[i][j]);

            }
printf ("\ n");
        }*/


int x=0;
//the bound of X
for (int i = 0; i < m; ++i) {
if (arr[i][0] > x) {
x = arr[i][0];
            }
        }
printf ("Bound x:%d\n", x);

//the bound of Y
int y=0;
for (int i = 0; i < m; ++i) {
if (arr[i][1] > y) {
y = arr[i][1];
            }
        }
printf ("Bound y:%d\n", y);


//min Xsum & ysum
int tmp_xmin=2000;
int tmp_ymin=2000;
int p = 0;

int q =0;
While (p<=x) {
int sumx = 0;
for (int i = 0; i < m; ++i) {
sumx + = ABS (arr[i][0]-p);
            }
if (Tmp_xmin > Sumx) {
tmp_xmin = sumx;
            }
p++;
        }

while (q<=y) {
int sumy = 0;
for (int i = 0; i < m; ++i) {
Sumy + = ABS (ARR[I][0]-Q);
}

Y
for (int i = 0; i < m; ++i) {
Sumy + = ABS (ARR[I][1]-P);
}
if (Tmp_ymin > Sumy) {
Tmp_ymin = Sumy;
}
q++;
}


printf ("tmp_xmin:%d\n", tmp_xmin);
printf ("tmp_ymin:%d\n", tmp_ymin);

sum = tmp_xmin + tmp_ymin;
printf ("summin:%d\n", sum);
}
return 0;
}

The shortest path of a block

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.