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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  int n, a, s, g;
  while (cin >> n >> a >> s >> g, n) {
    using P = pair<int, string>;
    vector<vector<P>> G(n);
    vector<vector<int>> rG(n);
    for (int i = 0; i < a; i++) {
      int x, y;
      string c;
      cin >> x >> y >> c;
      G[x].emplace_back(y, c);
      rG[y].push_back(x);
    }
    vector<int> r(n, 0);
    {
      queue<int> q;
      r[g] = 1;
      q.push(g);
      while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int u : rG[v]) {
          if (r[u])
            continue;
          r[u] = 1;
          q.push(u);
        }
      }
      if (!r[s]) {
        cout << "NO" << endl;
        continue;
      }
    }
    using T = tuple<string, int, int>;
    string INF = "{";
    vector<vector<string>> d(n, vector<string>(n * 20, INF));
    priority_queue<T, vector<T>, greater<T>> q;
    d[s][0] = "";
    q.emplace(d[s][0], s, 0);
    while (!q.empty()) {
      int v, l;
      tie(ignore, v, l) = q.top();
      q.pop();
      for (P p : G[v]) {
        int u = p.first;
        string c = d[v][l] + p.second;
        int nl = c.length();
        if (nl >= n * 20)
          continue;
        if (less<string>()(c, d[u][nl])) {
          d[u][nl] = c;
          q.emplace(d[u][nl], u, nl);
        }
      }
    }
    string ans = INF;
    for (int i = 0; i < n * 20; i++)
      ans = min(ans, d[g][i]);
    if (ans == INF || (int)ans.length() > n * 15)
      cout << "NO" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  int n, a, s, g;
  while (cin >> n >> a >> s >> g, n) {
    vector<vector<int>> rG(n);
    using T = tuple<int, int, string>;
    vector<string> b(n);
    vector<T> E(a);
    for (int i = 0; i < a; i++) {
      int x, y;
      string c;
      cin >> x >> y >> c;
      E[i] = T(x, y, c);
      if (x != y) {
        rG[y].push_back(x);
      } else {
        if (b[x] == "")
          b[x] = c;
        b[x] = min(b[x], c);
      }
    }
    queue<int> q;
    vector<int> r(n, 0);
    r[g] = 1;
    q.push(g);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int u : rG[v]) {
        if (r[u])
          continue;
        r[u] = 1;
        q.push(u);
      }
    }
    if (!r[s]) {
      cout << "NO" << endl;
      continue;
    }
    string INF = "{";
    vector<string> d(n, INF);
    vector<string> d2(n, INF);
    d[s] = d2[s] = "";
    int f = 0, cnt = 0;
    while (1) {
      string gs = d[g];
      int ff;
      while (1) {
        ff = 0;
        for (int i = 0; i < a; i++) {
          int x, y;
          string c;
          tie(x, y, c) = E[i];
          if (!r[x] || !r[y] || x == y || d[x] == INF)
            continue;
          if (less<string>()(d[x] + c, d[y])) {
            d[y] = d[x] + c;
            d2[y] = d[y];
            ff = 1;
            break;
          }
          if (less<string>()(d2[x] + c, d[y])) {
            d[y] = d2[x] + c;
            d2[y] = d[y];
            ff = 1;
            break;
          }
        }
        if (!ff)
          break;
      }
      // cout<<d[g]<<" "<<gs<<endl;
      if (!less<string>()(d[g], gs))
        break;

      // cout<<cnt<<" "<<a<<endl;
      if (cnt++ > a) {
        f = 1;
        break;
      }
      for (int i = 0; i < n; i++)
        d2[i] = d2[i] + b[i];

      // for(int i=0;i<n;i++) cout<<i<<":"<<d2[i]<<endl;
    }
    if (f)
      cout << "NO" << endl;
    else
      cout << d[g] << endl;
  }

  return 0;
}