alexjercan/bugnet huggingface dataset:
Problem p00750 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, a, s, g;
  while (cin >> n >> a >> s >> g && n != 0) {
    int LIMIT = 0;
    vector<string> ans(n);
    vector<vector<vector<string>>> edge(
        n, vector<vector<string>>(n)); // edge[i][j][k]  i->j :k番目のラベル
    for (int i = 0; i < a; i++) {
      int x, y;
      string r;
      cin >> x >> y >> r;
      edge[x][y].push_back(r);
      LIMIT += r.size() * 4;
    }

    queue<int> que;
    que.push(g);
    int count = 0;

    while (!que.empty()) { // 無限ループするよ！
      count++;
      int to = que.front();
      que.pop();
      for (int from = 0; from < n; from++) {
        for (int k = 0; k < edge[from][to].size(); k++) {
          if (from == g)
            continue;
          string rabel = edge[from][to][k];
          if (ans[from] == "" || ans[from] > rabel + ans[to]) {
            ans[from] = rabel + ans[to];
            que.push(from);
          }
        }
      }
      bool b = false;
      for (int i = 0; i < n; i++) {
        if (ans[i].size() >= LIMIT) {
          b = true;
          break;
        }
      }
      if (b)
        break;
    }
    if (ans[s].size() >= LIMIT / 4 || ans[s] == "")
      cout << "NO" << endl;
    else
      cout << ans[s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, a, s, g;
  while (cin >> n >> a >> s >> g && n != 0) {
    int LIMIT = 0;
    vector<string> ans(n);
    vector<vector<vector<string>>> edge(
        n, vector<vector<string>>(n)); // edge[i][j][k]  i->j :k番目のラベル
    for (int i = 0; i < a; i++) {
      int x, y;
      string r;
      cin >> x >> y >> r;
      edge[x][y].push_back(r);
      LIMIT += 2400;
    }

    queue<int> que;
    que.push(g);
    int count = 0;

    while (!que.empty()) { // 無限ループするよ！
      count++;
      int to = que.front();
      que.pop();
      for (int from = 0; from < n; from++) {
        for (int k = 0; k < edge[from][to].size(); k++) {
          if (from == g)
            continue;
          string rabel = edge[from][to][k];
          if (ans[from] == "" || ans[from] > rabel + ans[to]) {
            ans[from] = rabel + ans[to];
            que.push(from);
          }
        }
      }
      bool b = false;
      for (int i = 0; i < n; i++) {
        if (ans[i].size() >= LIMIT * 2) {
          b = true;
          break;
        }
      }
      if (b)
        break;
    }
    if (ans[s].size() >= LIMIT / 4 || ans[s] == "")
      cout << "NO" << endl;
    else
      cout << ans[s] << endl;
  }
  return 0;
}
Problem p00750 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int x[1000], y[1000];
string lab[1000];

int main() {
  int n, m, s, g;
  while (cin >> n >> m >> s >> g && n) {
    for (int i = 0; i < m; i++) {
      cin >> y[i] >> x[i] >> lab[i];
    }
    string memo[40];
    for (int i = 0; i < 40; i++)
      memo[i] = "~";
    memo[g] = "";
    int ref = 0;
    bool bad = 0;
    int p = 0;
    while (1) {
      bool f = false;
      for (int j = 0; j < m; j++) {
        if (memo[x[j]] == "~")
          continue;
        string cost = lab[j] + memo[x[j]];
        if (cost < memo[y[j]]) {
          memo[y[j]] = cost;
          f = true;
          if (s == y[j] && memo[y[j]].length() > 250) {
            bad = true;
            f = false;
            break;
          }
        }
      }
      if (!f)
        break;
      ref++;
      if (ref == 300) {
        break;
      }
    }
    if (bad || memo[s] == "~")
      cout << "NO" << endl;
    else
      cout << memo[s] << endl;
  }
}

Error: TLE

Fixed submission:

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

int x[1000], y[1000];
string lab[1000];

string r(string s) {
  reverse(s.begin(), s.end());
  return s;
}
int main() {
  int n, m, s, g;
  while (cin >> n >> m >> s >> g && n) {
    for (int i = 0; i < m; i++) {
      cin >> y[i] >> x[i] >> lab[i];
      lab[i] = r(lab[i]);
    }
    string memo[40];
    for (int i = 0; i < 40; i++)
      memo[i] = "~";
    memo[g] = "";
    int ref = 0;
    while (1) {
      bool f = false;
      for (int i = 0; i < 40; i++) {
        for (int j = 0; j < m; j++) {
          string cost = memo[x[j]] + lab[j];
          if (r(cost) < r(memo[y[j]])) {
            memo[y[j]] = cost;
            f = true;
          }
        }
      }
      if (!f) {
        if (memo[s] == "~")
          cout << "NO" << endl;
        else
          cout << r(memo[s]) << endl;
        break;
      }
      ref++;
      if (ref == n + 1) {
        cout << "NO" << endl;
        break;
      }
    }
  }
}
Problem p00750 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

int n, m, s, g;

const int N = 41;
const int T = (N + 5) * 2 * 6;

typedef pair<int, string> E;

vector<E> edge[N];

string dp[T][N];

void init() {
  rep(i, N) edge[i].clear();
  rep(i, T) rep(j, N) dp[i][j] = "~";
}

string solve() {
  init();

  rep(i, m) {
    int a, b;
    string c;
    cin >> a >> b >> c;
    edge[a].push_back(E(b, c));
  }

  dp[0][s] = "";

  string mini = "~";
  int maxlen = (n + 3) * 2 * 6;
  rep(i, maxlen) {
    rep(j, n) {
      rep(k, edge[j].size()) {
        E e = edge[j][k];
        string str = dp[i][j] + e.second;
        if (dp[i + e.second.size()][e.first] > str) {
          dp[i + e.second.size()][e.first] = str;
        }
        mini = min(str, mini);
      }
    }
  }

  string answer = "~";
  rep(i, maxlen) { answer = min(answer, dp[i][g]); }

  // if(answer!=mini)answer="NO";

  if (answer == "~")
    answer = "NO";
  if (answer.size() >= n * 6)
    answer = "NO";
  return answer;
}

int main() {

  while (1) {
    cin >> n >> m >> s >> g;
    if (n == 0)
      break;
    string str = solve();
    cout << str << endl;
    // break;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

int n, m, s, g;

const int N = 40;
const int T = (N + 1) * 2 * 6;

typedef pair<int, string> E;

vector<E> edge[N];

string dp[T][N];

void init() {
  rep(i, N) edge[i].clear();
  rep(i, T) rep(j, N) dp[i][j] = "~";
}

string solve() {
  init();

  rep(i, m) {
    int a, b;
    string c;
    cin >> a >> b >> c;
    edge[a].push_back(E(b, c));
  }

  dp[0][s] = "";

  int maxlen = (n + 1) * 2 * 6;
  rep(i, maxlen) {
    rep(j, n) {
      rep(k, edge[j].size()) {
        E e = edge[j][k];
        string str = dp[i][j] + e.second;
        if (dp[i + e.second.size()][e.first] > str) {
          dp[i + e.second.size()][e.first] = str;
        }
      }
    }
  }

  string answer = "~";
  rep(i, maxlen) { answer = min(answer, dp[i][g]); }

  if (answer == "~")
    answer = "NO";
  if (answer.size() > maxlen - 6)
    answer = "NO";
  return answer;
}

int main() {

  while (1) {
    cin >> n >> m >> s >> g;
    if (n == 0)
      break;
    cout << solve() << endl;
    // break;
  }
}
Problem p00750 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
struct Edge {
  int from, to;
  string spell;
  Edge(int f, int t, string s) : from(f), to(t), spell(s) {}
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ifstream cinu("1169_input.txt");
  int N, A, S, G;
  while (cin >> N >> A >> S >> G && N) {
    vector<Edge> es;
    int x_, y_;
    string s;
    rep(i, A) {
      cin >> x_ >> y_ >> s;
      es.push_back(Edge(y_, x_, s));
    }
    vector<string> dist(N);
    for (string &s : dist)
      s = "~";
    dist[G] = "";
    bool fail = false;
    rep(repnum, N * 6) {
      bool update = false, update_s = false;
      for (const Edge &e : es)
        if (dist[e.from] != "~" && dist[e.to] > e.spell + dist[e.from]) {
          dist[e.to] = e.spell + dist[e.from];
          update = true;
          if (e.to == S)
            update_s = true;
        }
      if (!update)
        break;
      if (repnum > N && update_s) {
        fail = true;
        break;
      }
    }
    if (dist[S] == "~" || fail)
      cout << "NO" << endl;
    else
      cout << dist[S] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
struct Edge {
  int from, to;
  string spell;
  Edge(int f, int t, string s) : from(f), to(t), spell(s) {}
};
bool check(vector<int> &l, vector<int> &f, int S, int G) {
  int now = S;
  while (now != G) {
    for (int &k : l)
      if (k == now)
        return true;
    if (now == f[now])
      return true;
    now = f[now];
  }
  return false;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ifstream cinu("1169_input.txt");
  int N, A, S, G;
  while (cin >> N >> A >> S >> G && N) {
    vector<Edge> es;
    int x_, y_;
    string s;
    rep(i, A) {
      cin >> x_ >> y_ >> s;
      es.push_back(Edge(y_, x_, s)); //??????
    }
    vector<string> dist(N);
    for (string &s : dist)
      s = "~";
    dist[G] = "";
    bool has_loop = false;
    vector<int> loop, from(N, -1);
    rep(repnum, N + 1) {
      bool update = false;
      for (const Edge &e : es)
        if (dist[e.from] != "~" && dist[e.to] > e.spell + dist[e.from]) {
          dist[e.to] = e.spell + dist[e.from];
          from[e.to] = e.from;
          update = true;
          if (repnum == N)
            loop.push_back(e.to);
        }
      if (!update)
        break;
      if (repnum == N)
        has_loop = true;
    }
    if (dist[S] == "~" || (has_loop && check(from, loop, S, G)))
      cout << "NO" << endl;
    else
      cout << dist[S] << endl;
  }
  return 0;
}
Problem p00750 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<string, int> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF "~"

int n, m, s, t;
vector<P> g[44];
string d[44][500];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n >> m >> s >> t;
    if (n == 0)
      break;
    rep(i, 44) g[i].clear();
    rep(i, m) {
      int a, b;
      string ss;
      cin >> a >> b >> ss;
      g[a].pb(P(ss, b));
    }
    rep(i, 44) rep(j, 500) d[i][j] = INF;
    d[s][0] = "";
    rep(j, 500) rep(i, n) {
      rep(k, g[i].size()) {
        P p = g[i][k];
        if (p.fi.length() + j < 500) {
          minch(d[p.se][j + p.fi.length()], d[i][j] + p.fi);
        }
      }
    }
    string res = INF;
    repl(j, 1, 401) { minch(res, d[t][j]); }
    bool ok = true;
    repl(j, 401, 500) {
      if (d[t][j] < res)
        ok = false;
    }
    if (res != INF && ok)
      cout << res << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<string, int> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF "~"

int n, m, s, t;
vector<P> g[44];
string d[44][4800];

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n >> m >> s >> t;
    if (n == 0)
      break;
    rep(i, 44) g[i].clear();
    rep(i, m) {
      int a, b;
      string ss;
      cin >> a >> b >> ss;
      g[a].pb(P(ss, b));
    }
    rep(i, 44) rep(j, 4800) d[i][j] = INF;
    d[s][0] = "";
    rep(j, 4800) rep(i, n) {
      rep(k, g[i].size()) {
        P p = g[i][k];
        if (p.fi.length() + j < 4800) {
          minch(d[p.se][j + p.fi.length()], d[i][j] + p.fi);
        }
      }
    }
    string res = INF;
    repl(j, 1, 2401) { minch(res, d[t][j]); }
    bool ok = true;
    repl(j, 2401, 4800) {
      if (d[t][j] < res)
        ok = false;
    }
    if (res != INF && ok)
      cout << res << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p00750 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n, a, star, goal;
  while (cin >> n >> a >> star >> goal && n != 0) {
    vector<pair<string, pair<int, int>>> g(a);
    for (int i = 0; i < a; i++) {
      cin >> g[i].second.first >> g[i].second.second >> g[i].first;
    }
    vector<string> dp(n, "{");
    dp[goal] = "";
    int cnt = 0;
    for (int i = 0; i < 900; i++) {
      string k = dp[star];
      for (int j = 0; j < a; j++) {
        if (dp[g[j].second.second] != "{") {
          dp[g[j].second.first] =
              min(dp[g[j].second.first], g[j].first + dp[g[j].second.second]);
        }
      }
      if (k != dp[star]) {
        cnt = i;
      }
    }
    if (cnt >= 400 || dp[star] == "{") {
      cout << "NO" << endl;
    } else {
      cout << dp[star] << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n, a, star, goal;
  while (cin >> n >> a >> star >> goal && n != 0) {
    vector<pair<string, pair<int, int>>> g(a);
    for (int i = 0; i < a; i++) {
      cin >> g[i].second.first >> g[i].second.second >> g[i].first;
    }
    vector<string> dp(n, "{");
    dp[goal] = "";
    int cnt = 0;
    for (int i = 0; i < 1400; i++) {
      string k = dp[star];
      for (int j = 0; j < a; j++) {
        if (dp[g[j].second.second] != "{") {
          dp[g[j].second.first] =
              min(dp[g[j].second.first], g[j].first + dp[g[j].second.second]);
        }
      }
      if (k != dp[star]) {
        cnt = i;
      }
    }
    if (cnt >= 800 || dp[star] == "{") {
      cout << "NO" << endl;
    } else {
      cout << dp[star] << endl;
    }
  }
  return 0;
}
Problem p00750 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 32
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct ES {
  int t;
  string s;
  bool operator<(ES a) const { return s > a.s; }
};

vector<ES> e[40];
bool r[40];
int n, a, s, g;
bool c[40][40];

void dfs(int x) {
  if (r[x]++)
    return;
  for (int i = 0; i < n; i++) {
    if (c[x][i]) {
      dfs(i);
    }
  }
}

int main() {
  while (cin >> n >> a >> s >> g, n | a | s | g) {
    for (int i = 0; i < n; i++) {
      e[i].clear();
      r[i] = false;
    }
    fill(c[0], c[n], false);
    for (int i = 0; i < a; i++) {
      int x, y;
      string l;
      cin >> x >> y >> l;
      ES a = {y, l};
      e[x].push_back(a);
      c[y][x] = true;
    }
    dfs(g);
    ES i = {s, ""};
    priority_queue<ES> que;
    que.push(i);
    set<string> s[40];
    while (!que.empty()) {
      ES c = que.top();
      if (c.t == g || c.s.size() > 6 * n)
        break;
      que.pop();
      if (!s[c.t].insert(c.s).second)
        continue;
      for (int i = 0; i < e[c.t].size(); i++) {
        ES n = {e[c.t][i].t, c.s + e[c.t][i].s};
        if (r[n.t]) {
          que.push(n);
        }
      }
    }
    if (que.empty() || que.top().s.size() > 6 * n) {
      cout << "NO" << endl;
    } else {
      cout << que.top().s << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

// 32
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

struct ES {
  int t;
  string s;
  bool operator<(ES a) const { return s > a.s; }
};

int main() {
  for (int n, a, s, g; cin >> n >> a >> s >> g, n | a | s | g;) {
    vector<ES> e[41];
    for (int i = 0; i < a; i++) {
      int x, y;
      string l;
      cin >> x >> y >> l;
      ES a = {y, l};
      e[x].push_back(a);
    }
    ES i = {s, ""};
    priority_queue<ES> que;
    que.push(i);
    while (!que.empty()) {
      ES c = que.top();
      if (c.t == g || c.s.size() > 6 * n)
        break;
      que.pop();
      for (int i = 0; i < e[c.t].size(); i++) {
        ES n = {e[c.t][i].t, c.s + e[c.t][i].s};
        que.push(n);
      }
    }
    if (que.empty() || que.top().s.size() > 6 * n) {
      cout << "NO" << endl;
    } else {
      cout << que.top().s << endl;
    }
  }
  return 0;
}
Problem p00750 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

struct Node {
  int p;
  string s;
  Node(int x, string y) {
    p = x;
    s = y;
  }
};

auto operator<(const Node &e, const Node &f) {
  if (e.s.compare(f.s) > 0)
    return true;
  else
    return false;
}

int main() {
  int n, a, s, g;
  while (cin >> n >> a >> s >> g, n) {
    vector<vector<Node>> G(n);
    vector<vector<int>> GG(n);
    for (int i = 0; i < a; ++i) {
      int x, y;
      string str;
      cin >> x >> y >> str;
      G[x].emplace_back(y, str);
      GG[y].push_back(x);
    }
    stack<int> st;
    vector<bool> memo(n);
    bool flag = false;
    st.push(g);
    memo[g] = 1;
    while (!st.empty()) {
      int now = st.top();
      st.pop();
      for (auto next : GG[now]) {
        if (!memo[next]) {
          st.push(next);
          memo[next] = 1;
        }
      }
    }
    vector<vector<string>> magic(n, vector<string>(310, ""));
    priority_queue<Node> pq;
    pq.emplace(s, "");
    while (!pq.empty()) {
      auto now = pq.top();
      if (now.p == g) {
        cout << now.s << endl;
        goto BREAK;
      }
      if (now.s.length() > 300) {
        // cout << now.s << endl;
        break;
      }
      pq.pop();
      for (auto n : G[now.p]) {
        string ss = now.s + n.s;
        string &mg = magic[n.p][ss.length()];
        if (mg.compare(ss) > 0 || mg == "") {
          if (!memo[n.p])
            continue;
          mg = ss;
          pq.emplace(n.p, ss);
        }
      }
    }
    cout << "NO" << endl;
  BREAK:;
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Node {
  int p;
  string s;
  Node(int x, string y) {
    p = x;
    s = y;
  }
};

auto operator<(const Node &e, const Node &f) {
  if (e.s.compare(f.s) > 0)
    return true;
  else
    return false;
}

int main() {
  int n, a, s, g;
  while (cin >> n >> a >> s >> g, n) {
    vector<vector<Node>> G(n);
    for (int i = 0; i < a; ++i) {
      int x, y;
      string str;
      cin >> x >> y >> str;
      G[x].emplace_back(y, str);
    }
    priority_queue<Node> pq;
    pq.emplace(s, "");
    while (!pq.empty()) {
      auto now = pq.top();
      if (now.p == g) {
        cout << now.s << endl;
        goto BREAK;
      }
      if (now.s.length() > 300)
        break;
      pq.pop();
      for (auto n : G[now.p]) {
        pq.emplace(n.p, now.s + n.s);
      }
    }
    cout << "NO" << endl;
  BREAK:;
  }
}
Problem p00750 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const string INF = "NO";

template <class T> struct Edge {
  int u, v;
  T w;
  Edge() {}
  Edge(int U, int V, T W) : u(U), v(V), w(W) {}
};

template <class T> struct AdjList : public vector<vector<Edge<T>>> {
  AdjList() {}
  AdjList(int n, const vector<Edge<T>> &v = vector<Edge<T>>())
      : vector<vector<Edge<T>>>(n, v) {}
  template <class U> AdjList(U b, U e) : vector<vector<Edge<T>>>(b, e) {}
};

template <class T> T BackwardBellmanFord(const AdjList<T> &adj, int s, int t) {
  int n = adj.size();
  vector<T> d(n, INF);
  d[t] = T();

  bool changed;
  rep(t, 6 * n + 1) {
    changed = false;
    rep(v, n) rep(i, adj[v].size()) {
      int u = adj[v][i].u;
      string nextd = adj[v][i].w + d[v];
      if (d[v] != INF && (d[u] == INF || nextd < d[u])) {
        d[u] = nextd;
        changed = true;
        if (u == s && t >= n)
          return INF;
      }
    }
    if (!changed)
      break;
  }

  return d[s];
}

int main() {
  for (int n, m, s, g; cin >> n >> m >> s >> g, n;) {
    AdjList<string> adj(n);
    rep(i, m) {
      int u, v;
      string lbl;
      cin >> u >> v >> lbl;
      adj[v].pb(Edge<string>(u, v, lbl)); // reverse edge
    }

    cout << BackwardBellmanFord(adj, s, g) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

template <class T> struct Edge {
  int u, v;
  T w;
  Edge() {}
  Edge(int U, int V, T W) : u(U), v(V), w(W) {}
};

template <class T> struct AdjList : public vector<vector<Edge<T>>> {
  AdjList() {}
  AdjList(int n, const vector<Edge<T>> &v = vector<Edge<T>>())
      : vector<vector<Edge<T>>>(n, v) {}
  template <class U> AdjList(U b, U e) : vector<vector<Edge<T>>>(b, e) {}
};

template <class T> T ReverseDijkstra(const AdjList<T> &adj, int s, int t) {
  int n = adj.size();
  vector<T> d(n, "~");
  d[t] = "";

  bool end = false;
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;
  pq.push(mp("", t));
  while (!pq.empty()) {
    pair<T, int> a = pq.top();
    pq.pop();
    int v = a.second;
    if (d[v] < a.first)
      continue;
    if (v == s) {
      if (end)
        return "NO";
      end = true;
    }

    rep(i, adj[v].size()) {
      int u = adj[v][i].u;
      T w = adj[v][i].w;
      T nextd = w + d[v];
      if (nextd < d[u]) {
        d[u] = nextd;
        pq.push(mp(d[u], u));
      }
    }
  }

  if (d[s] == "~")
    return "NO";
  return d[s];
}

int main() {
  for (int n, m, s, g; cin >> n >> m >> s >> g, n;) {
    AdjList<string> adj(n);
    rep(i, m) {
      int u, v;
      string lbl;
      cin >> u >> v >> lbl;
      adj[v].pb(Edge<string>(u, v, lbl)); // reverse edge
    }

    cout << ReverseDijkstra(adj, s, g) << endl;
  }

  return 0;
}