您的位置:首页 > 大数据 > 人工智能

UVA_10245_ The Closest Pair Problem

2016-04-15 15:52 393 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: