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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
using namespace std;

const int MAXN = 256;
const int MAXR = 1e3 + 16;

int N, M, R;
int dl[MAXN][MAXN], ds[MAXN][MAXN];
int path[MAXR];
int dp[2][MAXN];

int x, y, t;
char sl;

void floyd() {
  for (int k = 1; k <= N; k++) {
    for (int u = 1; u <= N; u++) {
      for (int v = 1; v <= N; v++) {
        if (dl[u][v] > dl[u][k] + dl[k][v])
          dl[u][v] = dl[u][k] + dl[k][v];
        if (ds[u][v] > ds[u][k] + ds[k][v])
          ds[u][v] = ds[u][k] + ds[k][v];
      }
    }
  }
}

int main() {
  // freopen("2200-input.txt", "r", stdin);
  // freopen("mine.txt", "w", stdout);
  while (cin >> N >> M, N || M) {
    memset(dl, 0x1f, sizeof(dl));
    memset(ds, 0x1f, sizeof(ds));
    for (int i = 1; i <= N; i++) {
      dl[i][i] = ds[i][i] = 0;
    }
    for (int i = 0; i < M; i++) {
      cin >> x >> y >> t >> sl;
      if (sl == 'L') {
        dl[x][y] = dl[y][x] = min(dl[x][y], t);
      } else {
        ds[x][y] = ds[y][x] = min(ds[x][y], t);
      }
    }
    floyd();
    cin >> R;
    for (int i = 1; i <= R; ++i) {
      cin >> path[i];
    }
    path[0] = path[1];
    memset(dp[0], 0x1f, sizeof(dp[0]));
    dp[0][path[0]] = 0;
    for (int i = 1; i <= R; ++i) {
      memset(dp[i & 1], 0x1f, sizeof(dp[i & 1]));
      for (int j = 1; j <= N; ++j) {
        for (int k = 1; k <= N; k++) {
          dp[i & 1][k] = min(dp[!(i & 1)][j] + dl[path[i - 1]][j] + ds[j][k] +
                                 dl[k][path[i]],
                             dp[i & 1][k]);
          if (j == k)
            dp[i & 1][k] =
                min(dp[i & 1][k], dp[!(i & 1)][j] + dl[path[i - 1]][path[i]]);
        }
      }
      // copy(dp[i & 1] + 1, dp[i & 1] + N + 1, ostream_iterator<int>(cout, "
      // ")); cout << "\\n";
    }
    cout << *min_element(dp[R & 1] + 1, dp[R & 1] + N + 1) << "\\n";
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAXN = 1e4 + 16;
const int MAXR = 1e3 + 16;

int N, M, R;
unsigned dl[MAXN][MAXN], ds[MAXN][MAXN];
int path[MAXR];
unsigned dp[2][MAXN];

int x, y, t;
char sl;

void floyd() {
  for (int k = 1; k <= N; k++) {
    for (int u = 1; u <= N; u++) {
      for (int v = 1; v <= N; v++) {
        if (dl[u][v] > dl[u][k] + dl[k][v])
          dl[u][v] = dl[u][k] + dl[k][v];
        if (ds[u][v] > ds[u][k] + ds[k][v])
          ds[u][v] = ds[u][k] + ds[k][v];
      }
    }
  }
}

int main() {
  // freopen("2200.txt", "r", stdin);
  while (cin >> N >> M, N | M) {
    memset(dl, 0x3f, sizeof(dl));
    memset(ds, 0x3f, sizeof(ds));
    for (int i = 1; i <= N; i++) {
      dl[i][i] = ds[i][i] = 0;
    }
    for (int i = 0; i < M; i++) {
      cin >> x >> y >> t >> sl;
      if (sl == 'L') {
        dl[x][y] = dl[y][x] = t;
      } else {
        ds[x][y] = ds[y][x] = t;
      }
    }
    floyd();
    /*for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            //cout << i << " " << j << " " << dl[i][j] << " " << ds[i][j] <<
    "\\n";
        }
    }*/
    cin >> R;
    for (int i = 1; i <= R; ++i) {
      cin >> path[i];
    }
    memset(dp[0], 0x3f, sizeof(dp[0]));
    dp[0][1] = 0;
    int prev = 1;
    for (int i = 1; i <= R; ++i) {
      memset(dp[i & 1], 0x3f, sizeof(dp[i & 1]));
      int to = path[i];
      for (int j = 1; j <= N; ++j) {
        for (int k = 1; k <= N; k++) {
          dp[i & 1][k] =
              min(dp[!(i & 1)][j] + dl[prev][j] + ds[j][k] + dl[k][to],
                  dp[i & 1][k]);
        }
      }
      prev = to;
    }
    cout << *min_element(dp[R & 1] + 1, dp[R & 1] + N + 1) << "\\n";
  }
}