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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define show(x) cerr << #x << " = " << x << endl
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\\n";
  return os;
}
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

struct Edge {
  int from;
  int to;
  string s;
};
struct Graph {
  Graph(const int n) : edge(n) {}
  void addEdge(const int from, const int to, const string &s) {
    edge[from].push_back(Edge{from, to, s});
  }
  vector<vector<Edge>> edge;
};

int main() {
  while (true) {
    int N, A, S, G;
    cin >> N >> A >> S >> G;
    if (N == 0 and A == 0 and S == 0 and G == 0) {
      break;
    }
    Graph g(N);
    g.addEdge(G, G, "");
    for (int i = 0; i < A; i++) {
      int x, y;
      string s;
      cin >> x >> y >> s;
      g.addEdge(x, y, s);
    }
    const string MAX = "{";
    const int TURN = 6 * N + 1;
    vector<vector<string>> dp(N);
    for (int i = 0; i < N; i++) {
      if (i == S) {
        dp[i] = vector<string>(g.edge[i].size(), "");
      } else {
        dp[i] = vector<string>(g.edge[i].size(), MAX);
      }
    }
    for (int i = 0; i <= TURN; i++) {
      vector<vector<string>> tmp(N);
      for (int j = 0; j < N; j++) {
        tmp[j] = vector<string>(g.edge[j].size(), MAX);
      }
      for (int j = 0; j < N; j++) {
        for (int k = 0; k < g.edge[j].size(); k++) {
          if (dp[j][k] == MAX) {
            continue;
          }
          const auto &e = g.edge[j][k];
          const string s = dp[j][k] + e.s;
          for (int l = 0; l < g.edge[e.to].size(); l++) {
            if (tmp[e.to][l] + g.edge[e.to][l].s > s + g.edge[e.to][l].s) {
              tmp[e.to][l] = s;
            }
          }
        }
      }
      dp = tmp;
    }
    const int MAXLEN = 6 * N;
    if (dp[G][0] == MAX or dp[G][0].size() > MAXLEN) {
      cout << "NO" << endl;
    } else {
      cout << dp[G][0] << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define show(x) cerr << #x << " = " << x << endl
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\\n";
  return os;
}
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

struct Edge {
  int from;
  int to;
  string s;
};
struct Graph {
  Graph(const int n) : edge(n) {}
  void addEdge(const int from, const int to, const string &s) {
    edge[from].push_back(Edge{from, to, s});
  }
  vector<vector<Edge>> edge;
};

int main() {
  while (true) {
    int N, A, S, G;
    cin >> N >> A >> S >> G;
    if (N == 0 and A == 0 and S == 0 and G == 0) {
      break;
    }
    Graph g(N);
    for (int i = 0; i < A; i++) {
      int x, y;
      string s;
      cin >> x >> y >> s;
      g.addEdge(x, y, s);
    }
    g.addEdge(G, G, "");
    const string MAX = "{";
    const int TURN = 4000;
    vector<string> dp(N, MAX);
    dp[S] = "";
    for (int i = 0; i < TURN; i++) {
      vector<string> tmp(N, MAX);
      for (int j = 0; j < N; j++) {
        if (dp[j] != MAX) {
          for (const auto &e : g.edge[j]) {
            tmp[e.to] = min(tmp[e.to], dp[j] + e.s);
          }
        }
      }
      dp = tmp;
    }
    constexpr int MAXLEN = 2400;
    if (dp[G] == MAX or dp[G].size() > MAXLEN) {
      cout << "NO" << endl;
    } else {
      cout << dp[G] << endl;
    }
  }
  return 0;
}