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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef pair<double, int> P;
typedef pair<int, int> PI;
typedef pair<double, PI> S;

double calc(P p, P q) {
  double x = p.first - q.first;
  double y = p.second - q.second;
  return sqrt(x * x + y * y);
}
int main() {
  int N;
  while (cin >> N && N) {
    int x[100], y[100];
    REP(i, N) cin >> x[i] >> y[i];
    vector<int> hv;
    REP(i, N) hv.push_back(y[i]);
    sort(hv.begin(), hv.end());
    hv.erase(unique(hv.begin(), hv.end()), hv.end());
    vector<P> ps;
    REP(i, N) ps.push_back(P(x[i], y[i]));
    REP(i, N - 1) {
      if (y[i] == y[i + 1])
        continue;
      int l = min(y[i], y[i + 1]);
      int r = max(y[i], y[i + 1]);
      REP(j, hv.size()) {
        int h = hv[j];
        if (l < h && h < r) {
          double px =
              x[i] + double(h - y[i]) / (y[i + 1] - y[i]) * (x[i + 1] - x[i]);
          ps.push_back(P(px, h));
        }
      }
    }
    sort(ps.begin(), ps.end());
    /// REP(i, ps.size()) printf("ps[%d] = (%f, %f)\\n", i, (double)ps[i].first,
    /// (double)ps[i].second);
    priority_queue<S, vector<S>, greater<S>> que;
    map<PI, double> dist;
    dist[PI(0, ps.size() - 1)] = 0;
    que.push(S(0, PI(0, ps.size() - 1)));
    while (!que.empty()) {
      S s = que.top();
      que.pop();
      int a = s.second.first, b = s.second.second;
      double c = s.first;
      if (dist[PI(a, b)] < c)
        continue;
      if (a == b) {
        printf("%.12f\\n", c);
        break;
      }
      for (int da = -1; da <= 1; da++) {
        for (int db = -1; db <= 1; db++) {
          int na = a + da;
          int nb = b + db;
          if (!valid(na, nb, ps.size(), ps.size()))
            continue;
          if (ps[na].second != ps[nb].second)
            continue;
          double nc = c;
          nc += calc(ps[a], ps[na]);
          nc += calc(ps[b], ps[nb]);
          // printf("na = %d nb = %d nc = %f\\n", na, nb, nc);
          if (!dist.count(PI(na, nb)) || dist[PI(na, nb)] > nc) {
            dist[PI(na, nb)] = nc;
            que.push(S(nc, PI(na, nb)));
          }
        }
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef pair<double, int> P;
typedef pair<int, int> PI;
typedef pair<double, PI> S;

double calc(P p, P q) {
  double x = p.first - q.first;
  double y = p.second - q.second;
  return sqrt(x * x + y * y);
}
int main() {
  int N;
  while (cin >> N && N) {
    int x[100], y[100];
    REP(i, N) cin >> x[i] >> y[i];
    vector<int> hv;
    REP(i, N) hv.push_back(y[i]);
    sort(hv.begin(), hv.end());
    hv.erase(unique(hv.begin(), hv.end()), hv.end());
    vector<P> ps;
    REP(i, N) ps.push_back(P(x[i], y[i]));
    REP(i, N - 1) {
      if (y[i] == y[i + 1])
        continue;
      int l = min(y[i], y[i + 1]);
      int r = max(y[i], y[i + 1]);
      REP(j, hv.size()) {
        int h = hv[j];
        if (l < h && h < r) {
          double px =
              x[i] + double(h - y[i]) / (y[i + 1] - y[i]) * (x[i + 1] - x[i]);
          ps.push_back(P(px, h));
        }
      }
    }
    sort(ps.begin(), ps.end());
    /// REP(i, ps.size()) printf("ps[%d] = (%f, %f)\\n", i, (double)ps[i].first,
    /// (double)ps[i].second);
    vector<vector<double>> dist(ps.size(), vector<double>(ps.size(), INF));
    dist[0][ps.size() - 1] = 0;
    priority_queue<S, vector<S>, greater<S>> que;
    que.push(S(0, PI(0, ps.size() - 1)));
    while (!que.empty()) {
      S s = que.top();
      que.pop();
      int a = s.second.first, b = s.second.second;
      double c = s.first;
      if (dist[a][b] < c)
        continue;
      if (a == b) {
        printf("%.12f\\n", c);
        break;
      }
      for (int da = -1; da <= 1; da++) {
        for (int db = -1; db <= 1; db++) {
          int na = a + da;
          int nb = b + db;
          if (!valid(na, nb, ps.size(), ps.size()))
            continue;
          if (ps[na].second != ps[nb].second)
            continue;
          double nc = c;
          nc += calc(ps[a], ps[na]);
          nc += calc(ps[b], ps[nb]);
          // printf("na = %d nb = %d nc = %f\\n", na, nb, nc);
          if (dist[na][nb] > nc) {
            dist[na][nb] = nc;
            que.push(S(nc, PI(na, nb)));
          }
        }
      }
    }
  }
  return 0;
}