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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const double EPS = 1.0e-10;

vector<int> x, y;

double dist(int i, int y1, int y2) {
  if (y1 == y2)
    return 0.0;
  return abs(sqrt(pow(y[i] - y[i + 1], 2.0) + pow(x[i] - x[i + 1], 2.0)) *
             (y1 - y2) / (y[i] - y[i + 1]));
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    x.assign(n + 2, -1);
    y.assign(n + 2, -1);
    for (int i = 1; i <= n; ++i)
      cin >> x[i] >> y[i];

    vector<vector<double>> dp(n + 2, vector<double>(n + 2, DBL_MAX));
    ;
    dp[1][n] = 0.0;
    multimap<double, pair<int, int>> mm;
    mm.insert(make_pair(0.0, make_pair(1, n)));

    for (;;) {
      double len = mm.begin()->first;
      int a = mm.begin()->second.first;
      int b = mm.begin()->second.second;
      mm.erase(mm.begin());
      if (len > dp[a][b] + EPS)
        continue;
      if (a == b) {
        printf("%.10f\\n", len);
        break;
      }

      int currY = y[a];

      for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
          int a1 = a + j - 1;
          int b1 = b + i;
          int nextY = y[b1];
          if (a1 < 1 || a1 > n || b1 < 1 || b1 > n)
            continue;
          if (min(y[a1], y[a1 + 1]) <= nextY &&
              nextY <= max(y[a1], y[a1 + 1])) {
            double len2 = len + dist(a1, currY, nextY) + dist(b, currY, nextY);
            if (b1 != b && y[b] == y[b1])
              len2 += abs(x[b1] - x[b]);
            if (a1 != a && y[a] == y[a1])
              len2 += abs(x[a1] - x[a]);
            if (len2 < dp[b1][a1] - EPS) {
              dp[b1][a1] = len2;
              mm.insert(make_pair(len2, make_pair(b1, a1)));
            }
          }
        }
      }

      for (int i = 0; i < 2; ++i) {
        int a1 = a + 2 * i - 1;
        int nextY = y[a1];
        if (a1 < 1 || a1 > n)
          continue;
        if (min(y[b], y[b + 1]) <= nextY && nextY <= max(y[b], y[b + 1])) {
          double len2 =
              len + dist(a - 1 + i, currY, nextY) + dist(b, currY, nextY);
          if (currY == nextY)
            len2 += abs(x[a1] - x[a]);
          if (len2 < dp[a1][b] - EPS) {
            dp[a1][b] = len2;
            mm.insert(make_pair(len2, make_pair(a1, b)));
          }
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const double EPS = 1.0e-10;

vector<int> x, y;

double dist(int i, int y1, int y2) {
  if (y1 == y2)
    return 0.0;
  return abs(sqrt(pow(y[i] - y[i + 1], 2.0) + pow(x[i] - x[i + 1], 2.0)) *
             (y1 - y2) / (y[i] - y[i + 1]));
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    // if(n == 0)
    //     return 0;
    n = 10;

    x.resize(n + 2, -1);
    y.resize(n + 2, -1);
    for (int i = 1; i <= n; ++i)
      cin >> x[i] >> y[i];

    for (int i = 1; i <= n; ++i) {
      x[i] = i - 1;
      y[i] = rand() % 100;
    }
    y[1] = y[n] = 0;

    vector<vector<double>> dp(n + 2, vector<double>(n + 2, DBL_MAX));
    ;
    dp[1][n] = 0.0;
    multimap<double, pair<int, int>> mm;
    mm.insert(make_pair(0.0, make_pair(1, n)));

    for (;;) {
      double len = mm.begin()->first;
      int a = mm.begin()->second.first;
      int b = mm.begin()->second.second;
      mm.erase(mm.begin());
      if (len > dp[a][b] + EPS)
        continue;
      if (a == b) {
        printf("%.10f\\n", len);
        break;
      }

      int currY = y[a];

      for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
          int a1 = a + j - 1;
          int b1 = b + i;
          int nextY = y[b1];
          if (a1 < 1 || a1 > n || b1 < 1 || b1 > n)
            continue;
          if (min(y[a1], y[a1 + 1]) <= nextY &&
              nextY <= max(y[a1], y[a1 + 1])) {
            double len2 = len + dist(a1, currY, nextY) + dist(b, currY, nextY);
            if (b1 != b && y[b] == y[b1])
              len2 += abs(x[b1] - x[b]);
            if (a1 != a && y[a] == y[a1])
              len2 += abs(x[a1] - x[a]);
            if (len2 < dp[b1][a1] - EPS) {
              dp[b1][a1] = len2;
              mm.insert(make_pair(len2, make_pair(b1, a1)));
            }
          }
        }
      }

      for (int i = 0; i < 2; ++i) {
        int a1 = a + 2 * i - 1;
        int nextY = y[a1];
        if (a1 < 1 || a1 > n)
          continue;
        if (min(y[b], y[b + 1]) <= nextY && nextY <= max(y[b], y[b + 1])) {
          double len2 =
              len + dist(a - 1 + i, currY, nextY) + dist(b, currY, nextY);
          if (currY == nextY)
            len2 += abs(x[a1] - x[a]);
          if (len2 < dp[a1][b] - EPS) {
            dp[a1][b] = len2;
            mm.insert(make_pair(len2, make_pair(a1, b)));
          }
        }
      }
    }
  }
}