Minimum Sum of Euclidean Distances to all given Points | C ++ Program to implement the above approach ; Function to calculate Euclidean distance ; Function to calculate the minimum sum of the euclidean distances to all points ; Calculate the centroid ; Calculate distance of all points ; Driver Code ; Initializing the points
#include <bits/stdc++.h> NEW_LINE using namespace std ; double find ( double x , double y , vector < vector < int > > & p ) { double mind = 0 ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) { double a = p [ i ] [ 0 ] , b = p [ i ] [ 1 ] ; mind += sqrt ( ( x - a ) * ( x - a ) + ( y - b ) * ( y - b ) ) ; } return mind ; } double getMinDistSum ( vector < vector < int > > & p ) { double x = 0 , y = 0 ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) { x += p [ i ] [ 0 ] ; y += p [ i ] [ 1 ] ; } x = x / p . size ( ) ; y = y / p . size ( ) ; double mind = find ( x , y , p ) ; return mind ; } int main ( ) { vector < vector < int > > vec = { { 0 , 1 } , { 1 , 0 } , { 1 , 2 } , { 2 , 1 } } ; double d = getMinDistSum ( vec ) ; cout << d << endl ; return 0 ; }