uva-1347

2020-10-05 19:00:20
#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>

#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;


vector<pair<int, int> > points;
vector<vector<double> > dp;
double dist(int a, int b) {
	return sqrt(pow(points[a].first - points[b].first, 2) + pow(points[a].second - points[b].second, 2));
}
int main()
{
	int n,a,b;
	
	while (cin >> n) {
		points.clear();
		dp.clear();

		points.push_back({ 0,0 });
		for (int i = 0; i < n; ++i) {
			cin >> a >> b;
			points.push_back(make_pair(a, b));
		}
		dp.assign(points.size()+1, vector<double>(points.size()+1, 0));

		for (int i = 1; i < n-1; ++i) {
			dp[n - 1][i] = dist(n - 1, n) + dist(i, n);
		}
		for (int i = n-2; i >= 1; --i) {
			for (int j = 1; j < i; ++j) {
				dp[i][j] = min(dp[i + 1][j] + dist(i, i + 1), dp[i + 1][i] + dist(j, i + 1));
			}
		}
		
		if (n == 1) cout << "0.00" << endl;
		else if (n == 2) cout << setiosflags(ios::fixed) << setprecision(2) << dist(1, 2) << endl;
		else {
			cout << setiosflags(ios::fixed) << setprecision(2) << dp[2][1] + dist(2,1) << endl;
		}
		
	}
	return 0;
}