UVA_10245_ The Closest Pair Problem

#include<iostream>        
#include<sstream>        
#include<string>        
#include<vector>        
#include<list>        
#include<set>        
#include<map>        
#include<stack>        
#include<queue>        
#include<algorithm>        
#include<cmath>        
#pragma warning(disable:4996)        
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
double divide_conquer(vector<pair<double,double>>point)
{
	if (point.size() == 1)
	{
		return 1000000000;
	}
	else if (point.size() == 2)
	{
		return sqrt(pow(point[0].first - point[1].first, 2) + pow(point[0].second - point[1].second, 2));
	}
	vector<pair<double, double>> point1;
	point1.assign(point.begin(), point.begin()+point.size()/2);
	vector<pair<double, double>> point2;
	point2.assign(point.begin() + point.size() / 2, point.end());
	auto min = std::min(divide_conquer(point1),divide_conquer(point2));
	for (size_t i = 0; i < point.size() / 2; i++)
	{
		for (size_t j = point.size() / 2; j < point.size(); j++)
		{
			min = std::min(min, sqrt(pow(point[i].first-point[j].first,2)+ pow(point[i].second - point[j].second, 2)));
		}
	}
	return min;
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	while (scanf("%d",&n)!=EOF&&n)
	{
		vector<pair<double, double>>point(n);
		for (int i = 0; i < n; i++)
		{
			scanf("%lf%lf", &point[i].first,&point[i].second);
		}
		/*std::sort(point.begin(), point.end());
		auto result = divide_conquer(point);*/
		double result = 10000.0;
		for (int i = 0; i < n; i++)
		{
			for (int j = i + 1; j < n; j++)
			{
				result = std::min(result, sqrt(pow(point[i].first - point[j].first, 2) + pow(point[i].second - point[j].second, 2)));
			}
		}
		if (result >= 10000)
		{
			printf("INFINITY\n");
		}
		else
		{
			printf("%.4lf\n", divide_conquer(point));
		}
	}
	return 0;
}

你可能感兴趣的:(uva,and,divide,conquer)