Problem p00888 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const double INF = 1e20;

typedef tuple<double, int, int> Pi;

int main() {
  int N, A[100], B[100];

  while (cin >> N, N) {
    vector<int> height;
    for (int i = 0; i < N; i++) {
      cin >> A[i] >> B[i];
      height.push_back(B[i]);
    }

    sort(begin(height), end(height));
    height.erase(unique(begin(height), end(height)), end(height));
    vector<pair<double, int>> beet;
    for (int h : height) {
      for (int i = 0; i < N - 1; i++) {
        if (min(B[i], B[i + 1]) < h && h < max(B[i], B[i + 1])) {
          double delta = (double)(A[i + 1] - A[i]) / (B[i + 1] - B[i]);
          beet.emplace_back(A[i] + delta * (h - B[i]), h);
        }
      }
    }
    for (int i = 0; i < N; i++) {
      beet.emplace_back(A[i], B[i]);
    }
    sort(begin(beet), end(beet));

    int sz = beet.size();
    map<pair<int, int>, double> v;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    que.emplace(0.0, 0, sz - 1);
    v[{0, sz - 1}] = 0;

    while (!que.empty()) {
      double cost;
      int l, r;
      tie(cost, l, r) = que.top();
      que.pop();
      if (cost > v[{l, r}])
        continue;
      if (l == r) {
        cout << fixed << setprecision(10) << cost << endl;
        break;
      }
      for (int i = max(l - 1, 0); i <= min(l + 1, sz - 1); i++) {
        for (int j = max(r - 1, 0); j <= min(r + 1, sz - 1); j++) {
          if (beet[i].second == beet[j].second) {
            double ncost = cost;
            ncost += sqrt(pow(beet[l].second - beet[i].second, 2) +
                          pow(beet[l].first - beet[i].first, 2));
            ncost += sqrt(pow(beet[r].second - beet[j].second, 2) +
                          pow(beet[r].first - beet[j].first, 2));
            if (v.count({i, j}) && v[{i, j}] <= ncost)
              continue;
            v[{i, j}] = ncost;
            que.emplace(ncost, i, j);
          }
        }
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const double INF = 1e20;

typedef tuple<double, int, int> Pi;

int main() {
  int N, A[100], B[100];

  while (cin >> N, N) {
    vector<int> height;
    for (int i = 0; i < N; i++) {
      cin >> A[i] >> B[i];
      height.push_back(B[i]);
    }

    sort(begin(height), end(height));
    height.erase(unique(begin(height), end(height)), end(height));
    vector<pair<double, int>> beet;
    for (int h : height) {
      for (int i = 0; i < N - 1; i++) {
        if (min(B[i], B[i + 1]) < h && h < max(B[i], B[i + 1])) {
          double delta = (double)(A[i + 1] - A[i]) / (B[i + 1] - B[i]);
          beet.emplace_back(A[i] + delta * (h - B[i]), h);
        }
      }
    }
    for (int i = 0; i < N; i++) {
      beet.emplace_back(A[i], B[i]);
    }
    sort(begin(beet), end(beet));

    int sz = beet.size();
    vector<vector<double>> v(beet.size(), vector<double>(beet.size(), INF));
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    que.emplace(0.0, 0, sz - 1);
    v[0][sz - 1] = 0;

    while (!que.empty()) {
      double cost;
      int l, r;
      tie(cost, l, r) = que.top();
      que.pop();
      if (cost > v[l][r])
        continue;
      if (l == r) {
        cout << fixed << setprecision(10) << cost << endl;
        break;
      }
      for (int i = max(l - 1, 0); i <= min(l + 1, sz - 1); i++) {
        for (int j = max(r - 1, 0); j <= min(r + 1, sz - 1); j++) {
          if (beet[i].second == beet[j].second) {
            double ncost = cost;
            ncost += sqrt(pow(beet[l].second - beet[i].second, 2) +
                          pow(beet[l].first - beet[i].first, 2));
            ncost += sqrt(pow(beet[r].second - beet[j].second, 2) +
                          pow(beet[r].first - beet[j].first, 2));
            if (v[i][j] <= ncost)
              continue;
            v[i][j] = ncost;
            que.emplace(v[i][j], i, j);
          }
        }
      }
    }
  }
}