Poj: 4091: The Closest M Points, poj4091

Source: Internet
Author: User

Poj: 4091: The Closest M Points, poj4091
Poj: 4091: The Closest M Points
Question

Description

Every meal, it's time for Mr. L to find out where to eat. Because there are too many delicious places to eat, and Xiao L is relatively lazy and does not want to go too far, so xiao L will first find the M restaurant nearest to him and then filter again.

The current location of the small L and the location of each restaurant are represented by the point in the same Cartesian coordinate system, and the distance between the point and the point is Euclidean distance, for the point p = (p1, p2 ,..., pn) and q = (q1, q2 ,..., qn), the distance between the two is defined as follows:

The coordinates of the location of the Small and Medium-sized L in the K-dimension space and the location of n restaurants are provided. Please help the small L to fulfill his needs.


Input
The first row contains two integers, n and K, 1 ≤ n ≤ 1st, and 1 ≤ K ≤ 5.
In the next n rows, each row contains K numbers, indicating the coordinates of each restaurant.
The next line contains a number t, 1 ≤ t ≤ 10000, indicating the number of small L queries.
Each query contains two rows. The first row contains K numbers, indicating the coordinates of the small L. The first row contains a number M, 1 ≤ M ≤ 10.
All coordinate values do not exceed 10000.
The input data contains multiple groups of data. process them one by one until the end of the file.
Output
Output m + 1 rows for each query:
1st row output: "the closest M points are:", where M is given in the input.
Next, the M line outputs the coordinates of M restaurants closest to each other, and outputs the coordinates from near to far.
The output data must be unique. Ensure that the locations of M + 1 restaurants vary from small to nearest, which indicates the following input data:
2 2
1 1
3 3
1
2 2
1
No.
Sample Input
3 21 11 33 422 322 31
Sample output
the closest 2 points are:1 33 4the closest 1 points are:1 3
Solution for solving the problem, this question is relatively simple. We need to convert the problem. This is to find the maximum or minimum value of k. The Brute Force Law is to sort the problem first and then find the first m min shortcuts. It is to build a small top heap of m scale, then update all the data, and the final elements of all the heaps are the results.
Code (brute force code)
# Include <iostream> # include <fstream> # include <list> # include <math. h ># include <algorithm> using namespace std; typedef pair <double, int> distance_restaurant; void read_data (); // data is the restaurant address table list <distance_restaurant> main_solution (int ** data, int n, int k, int * dizhi); int main () {read_data (); system ("pause"); return 0;} void read_data () {ifstream reader; reader. open ("data.txt"); int n, k; reader> n; reader> k; int ** Data = new int * [n]; for (int I = 0; I <n; I ++) {data [I] = new int [k]; for (int j = 0; j <k; j ++) reader> data [I] [j];} int t; reader> t; while (t> 0) {t --; int * dizhi = new int [k]; for (int I = 0; I <k; I ++) reader> dizhi [I]; int m; reader> m; cout <"the closest" <m <"points are:" <endl; list <distance_restaurant> result = main_solution (data, n, k, dizhi); for (list <distance_restaurant >:: iterator it = result. begin (); m> 0 & & It! = Result. end (); m --, it ++) {for (int j = 0; j <k; j ++) {cout <data [it-> second] [j] <"" ;}cout <endl ;}}} // data is the restaurant address table list <distance_restaurant> main_solution (int ** data, int n, int k, int * dizhi) {list <distance_restaurant> result; distance_restaurant elem; for (int I = 0; I <n; I ++) {elem. second = I; elem. first = 0; for (int j = 0; j <k; j ++) {elem. first + = (data [I] [j]-dizhi [j]) * (data [I] [j]-dizhi [j]);} elem. first = sqrt (elem. first); result. push_back (elem);} stable_sort (result. begin (), result. end (); return result ;}




All the test cases of poj1470 are passed. Why do I always prompt wrong answer when submitting them? The Code is as follows:

You have a problem with this algorithm... people ask about Closest Common Ancestors and Common Ancestors.
 

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.