A race is going to be held soon in college. Each racer will be assigned 3 random points from a set of N distinct points, the ith point is denoted by (Xi,Yi). In order to complete the race, each person has to run on a triangular track, assuming the 3 assigned points to be the corners of the track.
Raghav being the lazy participant wanted to run on the track with the least distance among all the possible tracks. Now, he wants your help to find out what is the smallest distance he has to run in order to finish the race.
Input
The first line contains an integer N(3≤N≤100)
N(3≤N≤100) — the number of points.
This is followed by N lines, each containing two integer Xi and Yi (−10^9≤Xi,Yi≤10^9)
— the coordinates of the ith point.
Output
Print a single real number d where d is the perimeter of the smallest track.
#include <iostream>
using namespace std;
double distance(long x1, long y1, long x2, long y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
int main()
{
int n;
cin >> n;
long* x = new long[n];
long* y = new long[n];
for (int i = 0; i < n; i++)
{
cin >> x[i];
cin >> y[i];
}
double minP = DBL_MAX;
for (int i = 0; i < n - 2; i++)
for (int j = i + 1; j < n - 1; j++)
for (int k = j + 1; k < n; k++)
{
double p = 0;
p += distance(x[i], y[i], x[j], y[j]);
p += distance(x[i], y[i], x[k], y[k]);
p += distance(x[j], y[j], x[k], y[k]);
if (p < minP)
minP = p;
}
//cout.precision(3); // precision of output was not specified; if specific precision is needed then uncomment this line and replace 3 with the needed precision
cout << minP;
delete[] x;
delete[] y;
return 0;
}
Comments
Leave a comment