Language: C++

#include <iostream>
#include <queue>
#include <vector>
#include <cmath>

using namespace std;

using pii = pair<int, int>;
constexpr double INF = (1LL << 55);

double dist(double x1, double y1, double x2, double y2)
{
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

struct State {
    double d;
    int l, r;

    State () {}
    State (double d, int l, int r) : d{d}, l{l}, r{r} {}

    bool operator < (const State &s) const {
        return d > s.d;
    }
};

double dijkstra(const vector<double>& X, const vector<double>& Y)
{
    int N = X.size();
    vector<vector<double>> d(N, vector<double>(N, INF));
    d[0][N - 1] = 0;

    priority_queue<State> pq;
    pq.push(State(0, 0, N - 1));

    while (!pq.empty()) {
        State s = pq.top(); pq.pop();
        int l = s.l, r = s.r;
        
        if (l > r) continue;
        if (d[l][r] < s.d) continue;
        if (l == r) return d[l][r];

        for (int i : {-1, +0, +1}) {
            int nl = l + i;
            for (int j : {-1, +0, +1}) {
                int nr = r + j;
                if (nl < 0 || nl >= N ||
                    nr < 0 || nr >= N) continue;

                if (Y[nl] != Y[nr]) continue;
                
                double ld = dist(X[l], Y[l], X[nl], Y[nl]);
                double rd = dist(X[r], Y[r], X[nr], Y[nr]);
                double nd = s.d + ld + rd;
                
                if (nd < d[nl][nr]) {
                    d[nl][nr] = nd;
                    pq.push(State(nd, nl, nr));
                }               
            }
        }       
    }
    return -1;
}

int main()
{
    int N;
    while (1) {
        cin >> N;
        if (N == 0) break;
        vector<int> x(N), y(N);
        for (int i = 0; i < N; i++) {
            cin >> x[i] >> y[i];            
        }
        
        vector<pair<double, double>> v;
        for (int i = 0; i < N - 1; i++) {
            double slope;
            if (y[i] != y[i + 1]) {
                slope = (double)(x[i + 1] - x[i]) / (y[i + 1] - y[i]);
            } else {
                slope = 1;
            }
                
            if (y[i] < y[i + 1]) {
                for (int j = y[i]; j <= y[i + 1]; j++) {
                    v.emplace_back(make_pair(x[i] + slope * (j - y[i]), j));
                }
            } else {
                for (int j = y[i]; j >= y[i + 1]; j--) {
                    v.emplace_back(make_pair(x[i] - slope * (y[i] - j), j));
                }
            }
        }

        v.emplace_back(make_pair(x[N - 1], y[N - 1]));

        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        
        vector<double> X, Y;
        for (auto& p : v) {
            X.emplace_back(p.first);
            Y.emplace_back(p.second);
        }
            
        printf("%.10f\n", dijkstra(X, Y));
    }        
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:101:9: error: 'sort' was not declared in this scope; did you mean 'sqrt'?
  101 |         sort(v.begin(), v.end());
      |         ^~~~
      |         sqrt
a.cc:102:17: error: 'unique' was not declared in this scope
  102 |         v.erase(unique(v.begin(), v.end()), v.end());
      |                 ^~~~~~