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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;
const LL INF = 1LL << 55;

vector<vector<LL>> sea, land;
vector<int> z;
int n, m, r;
vector<vector<LL>> dp;

LL dfs(int cur, int ship) {
  LL ret = dp[cur][ship];
  if (ret >= 0)
    return ret;
  if (cur == r - 1)
    return 0;
  ret = land[z[cur]][z[cur + 1]] + dfs(cur + 1, ship);
  for (int i = 0; i < n; i++) {
    LL tmp = land[z[cur]][ship] + sea[ship][i] + land[i][z[cur + 1]];
    if (tmp >= INF)
      continue;
    tmp += dfs(cur + 1, i);
    ret = min(ret, tmp);
  }
  dp[cur][ship] = ret;
  return ret;
}

int main() {
  while (cin >> n >> m, n | m) {
    sea.clear();
    land.clear();
    z.clear();
    dp.clear();
    sea.resize(n, vector<LL>(n, INF));
    land.resize(n, vector<LL>(n, INF));

    for (int i = 0; i < n; i++) {
      sea[i][i] = land[i][i] = 0;
    }

    for (int i = 0; i < m; i++) {
      int tx, ty, tt;
      char tsl;
      cin >> tx >> ty >> tt >> tsl;
      tx--;
      ty--;
      if (tsl == 'L') {
        land[tx][ty] = tt;
        land[ty][tx] = tt;
      } else {
        sea[tx][ty] = tt;
        sea[ty][tx] = tt;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          land[j][k] = min(land[j][k], land[j][i] + land[i][k]);
          sea[j][k] = min(sea[j][k], sea[j][i] + sea[i][k]);
        }
      }
    }

    cin >> r;
    z.resize(r);
    for (int i = 0; i < r; i++) {
      int tz;
      cin >> tz;
      z[i] = tz - 1;
    }
    dp.resize(r, vector<LL>(n, -1));
    cout << dfs(0, z[0]) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;
const LL INF = 1LL << 55;

vector<vector<LL>> sea, land;
vector<int> z;
int n, m, r;

LL dfs(int cur, int ship) {
  // cout << cur << " " << ship << endl;
  if (cur == r - 1)
    return 0;
  LL ret = land[z[cur]][z[cur + 1]] + dfs(cur + 1, ship);
  for (int i = 0; i < n; i++) {
    LL tmp = land[z[cur]][ship] + sea[ship][i] + land[i][z[cur + 1]];
    if (tmp >= INF)
      continue;
    tmp += dfs(cur + 1, i);
    ret = min(ret, tmp);
  }
  return ret;
}

int main() {
  while (cin >> n >> m, n | m) {
    sea.clear();
    land.clear();
    z.clear();
    sea.resize(n, vector<LL>(n, INF));
    land.resize(n, vector<LL>(n, INF));

    for (int i = 0; i < n; i++) {
      sea[i][i] = land[i][i] = 0;
    }

    for (int i = 0; i < m; i++) {
      int tx, ty, tt;
      char tsl;
      cin >> tx >> ty >> tt >> tsl;
      tx--;
      ty--;
      if (tsl == 'L') {
        land[tx][ty] = tt;
        land[ty][tx] = tt;
      } else {
        sea[tx][ty] = tt;
        sea[ty][tx] = tt;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          land[j][k] = min(land[j][k], land[j][i] + land[i][k]);
          sea[j][k] = min(sea[j][k], sea[j][i] + sea[i][k]);
        }
      }
    }

    cin >> r;
    z.resize(r);
    for (int i = 0; i < r; i++) {
      int tz;
      cin >> tz;
      z[i] = tz - 1;
    }

    cout << dfs(0, z[0]) << endl;
  }

  return 0;
}