UVA_216_Getting in Line_brute_force

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#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;
using std::priority_queue;
using std::sort;
using std::next_permutation;
bool compare(const pair<double,double>&a, const pair<double, double>&b)
{
	if (a.first != b.first)
	{
		return a.first < b.first;
	}
	return a.second < b.second;
}
double sum(const vector<pair<double, double>>&position)
{
	double ret = 0.0;
	for (size_t i = 0; i != position.size()-1; i++)
	{
		//cout << sqrt(pow(fabs(position[i].first - position[i + 1].first), 2) + pow(fabs(position[i].second - position[i + 1].second), 2)) + 16.0 << endl;
		ret += sqrt(pow(fabs(position[i].first - position[i + 1].first), 2) + pow(fabs(position[i].second - position[i + 1].second), 2))+16.0;
	}
	return ret;
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n,count=0;
	while (cin >> n)
	{
		if (!n)
		{
			break;
		}
		vector<pair<double, double>>position;
		while (n--)
		{
			double x, y; cin >> x >> y;
			position.push_back({ x,y });
		}
		sort(position.begin(), position.end(), compare);
		auto i = n*(n - 1) / 2;
		double length = 2000000000;
		auto mini = position;
		do
		{
			auto curlength = sum(position);
			if (length > curlength)
			{
				length = curlength;
				mini = position;
			}
		} while (next_permutation(position.begin(), position.end()));
		printf("**********************************************************\n");
		printf("Network #%d\n", ++count);
		for (size_t i = 0; i != mini.size() - 1; i++)
		{
			printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet.\n",
				(int)mini[i].first, (int)mini[i].second, (int)mini[i + 1].first, (int)mini[i + 1].second,
				sqrt(pow(fabs(mini[i].first - mini[i + 1].first), 2) + pow(fabs(mini[i].second - mini[i + 1].second), 2)) + 16.0
				);
			
		}
		printf("Number of feet of cable required is %.2lf.\n",sum(mini));
	}
	return 0;
}



你可能感兴趣的:(uva,Force,brute)