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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#define N_MAX 105
#define X_MAX 10010
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
typedef pair<double, P> P1;
void dijkstra();
double Cal(int, int);
int n, a[N_MAX], r;
double b, v, e, f, dp[X_MAX][N_MAX];

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cin >> b >> r >> v >> e >> f;
    dijkstra();
  }
  return 0;
}

void dijkstra() {
  for (int i = 0; i < X_MAX; i++)
    for (int j = 0; j < n; j++)
      dp[i][j] = INF;
  priority_queue<P1, vector<P1>, greater<P1>> Q;
  dp[0][0] = Cal(0, a[0]);
  Q.push(make_pair(dp[0][0], make_pair(a[0], 1)));
  while (!Q.empty()) {
    P1 v = Q.top();
    Q.pop();
    double t = v.first;
    int uy = v.second.first, ux = v.second.second;
    int ncny = uy + a[ux] - a[ux - 1];
    double nccal = Cal(uy, ncny);
    if (ncny < 0)
      continue;
    if (dp[ncny][ux] > t + nccal) {
      dp[ncny][ux] = t + nccal;
      if (ux != n - 1)
        Q.push(make_pair(dp[ncny][ux], make_pair(ncny, ux + 1)));
    }
    int cny = a[ux] - a[ux - 1];
    if (cny < 0)
      continue;
    double ccal = Cal(0, cny) + b;
    if (dp[cny][ux] > t + ccal) {
      dp[cny][ux] = t + ccal;
      if (ux != n - 1)
        Q.push(make_pair(dp[cny][ux], make_pair(cny, ux + 1)));
    }
  }
  double mincost = INF;
  for (int i = 0; i <= a[n - 1]; i++)
    mincost = min(mincost, dp[i][n - 1]);
  printf("%.4f\\n", mincost);
}

double Cal(int begin, int end) {
  double ret = 0;
  for (int i = begin; i < end; i++) {
    if (i >= r)
      ret += 1.0 / (1.0 * v - 1.0 * e * (1.0 * i - 1.0 * r));
    else
      ret += 1.0 / (1.0 * v - 1.0 * f * (1.0 * r - 1.0 * i));
  }
  return ret;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define N_MAX 105
#define X_MAX 100010
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;
typedef pair<double, P> P1;
void dijkstra();
double Cal(int, int);
int n, a[N_MAX], r;
double b, v, e, f, dp[X_MAX][N_MAX];

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cin >> b >> r >> v >> e >> f;
    dijkstra();
  }
  return 0;
}

void dijkstra() {
  for (int i = 0; i < X_MAX; i++)
    for (int j = 0; j < n; j++)
      dp[i][j] = INF;
  priority_queue<P1, vector<P1>, greater<P1>> Q;
  dp[0][0] = Cal(0, a[0]);
  Q.push(make_pair(dp[0][0], make_pair(a[0], 1)));
  while (!Q.empty()) {
    P1 v = Q.top();
    Q.pop();
    double t = v.first;
    int uy = v.second.first, ux = v.second.second;
    int ncny = uy + a[ux] - a[ux - 1];
    double nccal = Cal(uy, ncny);
    if (dp[ncny][ux] > t + nccal) {
      dp[ncny][ux] = t + nccal;
      if (ux != n - 1)
        Q.push(make_pair(dp[ncny][ux], make_pair(ncny, ux + 1)));
    }
    int cny = a[ux] - a[ux - 1];
    double ccal = Cal(0, cny) + b;
    if (dp[cny][ux] > t + ccal) {
      dp[cny][ux] = t + ccal;
      if (ux != n - 1)
        Q.push(make_pair(dp[cny][ux], make_pair(cny, ux + 1)));
    }
  }
  double mincost = INF;
  for (int i = 0; i <= a[n - 1]; i++)
    mincost = min(mincost, dp[i][n - 1]);
  cout << mincost << endl;
}

double Cal(int begin, int end) {
  double ret = 0;
  for (int i = begin; i < end; i++) {
    if (i >= r)
      ret += 1.0 / (1.0 * v - 1.0 * e * (1.0 * i - 1.0 * r));
    else
      ret += 1.0 / (1.0 * v - 1.0 * f * (1.0 * r - 1.0 * i));
  }
  return ret;
}