alexjercan/bugnet huggingface dataset:
Problem p00749 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

char p[64][16];
bool visit[64][16];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};

bool dfs(int y, int x, int &rn, int &rd) {
  int n = 0, d = 0;
  int xl = 100, xr = -100;
  char ch = p[y][x];

  queue<int> q;
  q.push(y << 8 | x);
  visit[y][x] = true;
  while (!q.empty()) {
    y = q.front() >> 8;
    x = q.front() & 0xff;
    q.pop();

    n += x;
    ++d;

    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!visit[ny][nx] && p[ny][nx] == ch) {
        visit[ny][nx] = true;
        q.push(ny << 8 | nx);
      }
    }

    if (!visit[y - 1][x] && isdigit(p[y - 1][x])) {
      if (!dfs(y - 1, x, n, d)) {
        return false;
      }
    }

    if (p[y + 1][x] != ch) {
      if (p[y + 1][x] == '!' || isdigit(p[y + 1][x])) {
        xl = min(xl, x);
        xr = max(xr, x + 1);
      }
    }
  }

  rn += n;
  rd += d;

  // xl < n / d + 0.5 < xr
  return 2 * d * xl < 2 * n + d && 2 * n + d < 2 * d * xr;
}

int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    memset(p, '.', sizeof p);
    memset(p[h + 1], '!', sizeof p[h + 1]);
    memset(visit, 0, sizeof visit);

    for (int i = 1; i <= h; ++i) {
      scanf(" %s", p[i] + 1);
    }

    int x;
    for (x = 1; !isdigit(p[h][x]); ++x)
      ;
    int n = 0, d = 0;
    puts(dfs(h, x, n, d) ? "STABLE" : "UNSTABLE");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

char p[16][64];
bool visit[16][64];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};

bool dfs(int y, int x, int &rn, int &rd) {
  int n = 0, d = 0;
  int xl = 100, xr = -100;
  char ch = p[y][x];

  queue<int> q;
  q.push(y << 8 | x);
  visit[y][x] = true;
  while (!q.empty()) {
    y = q.front() >> 8;
    x = q.front() & 0xff;
    q.pop();

    n += x;
    ++d;

    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i], nx = x + dx[i];
      if (!visit[ny][nx] && p[ny][nx] == ch) {
        visit[ny][nx] = true;
        q.push(ny << 8 | nx);
      }
    }

    if (!visit[y - 1][x] && isdigit(p[y - 1][x])) {
      if (!dfs(y - 1, x, n, d)) {
        return false;
      }
    }

    if (p[y + 1][x] != ch) {
      if (p[y + 1][x] == '!' || isdigit(p[y + 1][x])) {
        xl = min(xl, x);
        xr = max(xr, x + 1);
      }
    }
  }

  rn += n;
  rd += d;

  // xl < n / d + 0.5 < xr
  return 2 * d * xl < 2 * n + d && 2 * n + d < 2 * d * xr;
}

int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    memset(p, '.', sizeof p);
    memset(p[h + 1], '!', sizeof p[h + 1]);
    memset(visit, 0, sizeof visit);

    for (int i = 1; i <= h; ++i) {
      scanf(" %s", p[i] + 1);
    }

    int x;
    for (x = 1; !isdigit(p[h][x]); ++x)
      ;
    int n = 0, d = 0;
    puts(dfs(h, x, n, d) ? "STABLE" : "UNSTABLE");
  }
}
Problem p00750 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX (6 * N)

class Node {
public:
  vector<int> to;
  vector<string> sp;
  Node() {}
};

class State {
public:
  int p;
  string str;
  State(int p, string str) : p(p), str(str) {}

  bool operator<(const State &s) const { return str > s.str; }
};

int main() {

  //	ifstream cin("E1.txt");
  //	ofstream cout("out.txt");
  int N, A, S, G;
  while (cin >> N >> A >> S >> G, (N || A || S || G)) {
    Node node[45];
    for (int i = 0; i < A; i++) {
      int s, d;
      string str;
      cin >> s >> d >> str;
      node[s].to.push_back(d);
      node[s].sp.push_back(str);
    }

    bool v[40][500] = {0};
    priority_queue<State> q;
    q.push(State(S, ""));

    bool g = false;
    while (!q.empty()) {
      State s = q.top();
      q.pop();

      if (v[s.p][s.str.size()])
        continue;
      v[s.p][s.str.size()] = 1;

      if (s.p == G) {
        if (s.str.size() > MAX)
          break;

        g = true;
        cout << s.str << endl;
        break;
      }

      for (int i = 0; i < node[s.p].to.size(); i++) {
        int next = node[s.p].to[i];
        string spl = node[s.p].sp[i];

        if (s.str.size() + spl.size() > MAX * 2)
          continue;
        if (v[next][s.str.size() + spl.size()])
          continue;

        q.push(State(next, s.str + spl));
      }
    }
    if (!g)
      cout << "NO" << endl;
  }
}

Error: WA

Fixed submission:

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

#define MAX (6 * N)

class Node {
public:
  vector<int> to;
  vector<string> sp;
  Node() {}
};

class State {
public:
  int p;
  string str;
  State(int p, string str) : p(p), str(str) {}

  bool operator<(const State &s) const { return str > s.str; }
};

int main() {

  ifstream cin("E1.txt");
  ofstream cout("out.txt");
  int N, A, S, G;
  while (cin >> N >> A >> S >> G, (N || A || S || G)) {
    Node node[45];
    for (int i = 0; i < A; i++) {
      int s, d;
      string str;
      cin >> s >> d >> str;
      node[s].to.push_back(d);
      node[s].sp.push_back(str);
    }

    bool v[40][500] = {0};
    priority_queue<State> q;
    q.push(State(S, ""));

    bool g = false;
    while (!q.empty()) {
      State s = q.top();
      q.pop();

      if (v[s.p][s.str.size()])
        continue;
      v[s.p][s.str.size()] = 1;

      if (s.p == G) {
        if (s.str.size() > MAX)
          break;

        g = true;
        cout << s.str << endl;
        break;
      }

      for (int i = 0; i < node[s.p].to.size(); i++) {
        int next = node[s.p].to[i];
        string spl = node[s.p].sp[i];

        if (s.str.size() + spl.size() > MAX * 2)
          continue;
        if (v[next][s.str.size() + spl.size()])
          continue;

        q.push(State(next, s.str + spl));
      }
    }
    if (!g)
      cout << "NO" << endl;
  }
}
Problem p00750 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int n, m, s, g;
vector<pair<int, string>> edge[45];
string dp[45];
struct S {
  int v;
  string s;
  bool operator<(const S &a) const { return s > a.s; }
  S(int a, string b) {
    v = a;
    s = b;
  }
};
int a[3005], b[3005];
string c[3005];
int main() {
  while (1) {
    scanf("%d%d%d%d", &n, &m, &s, &g);
    if (n == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      edge[i].clear();
      dp[i] = "1";
    }
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i] >> c[i];
    }
    dp[g] = "";
    bool bad = 0;
    for (int t = 0; t < 1000; t++) {
      bool upd = 0;
      for (int i = 0; i < m; i++) {
        if (dp[b[i]] != "1" &&
            (dp[a[i]] == "1" || dp[a[i]] > c[i] + dp[b[i]])) {
          dp[a[i]] = c[i] + dp[b[i]];
          upd = 1;
          if (t > 500 && a[i] == s)
            bad = 1;
        }
      }
    }
    if (bad || dp[s] == "1")
      puts("NO");
    else
      cout << dp[s] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int n, m, s, g;
vector<pair<int, string>> edge[45];
string dp[45];
struct S {
  int v;
  string s;
  bool operator<(const S &a) const { return s > a.s; }
  S(int a, string b) {
    v = a;
    s = b;
  }
};
int a[3005], b[3005];
string c[3005];
int main() {
  while (1) {
    scanf("%d%d%d%d", &n, &m, &s, &g);
    if (n == 0)
      return 0;
    for (int i = 0; i < n; i++) {
      edge[i].clear();
      dp[i] = "1";
    }
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i] >> c[i];
    }
    dp[g] = "";
    bool bad = 0;
    for (int t = 0; t < 3000; t++) {
      bool upd = 0;
      for (int i = 0; i < m; i++) {
        if (dp[b[i]] != "1" &&
            (dp[a[i]] == "1" || dp[a[i]] > c[i] + dp[b[i]])) {
          dp[a[i]] = c[i] + dp[b[i]];
          upd = 1;
          if (t > 2000 && a[i] == s)
            bad = 1;
        }
      }
    }
    if (bad || dp[s] == "1")
      puts("NO");
    else
      cout << dp[s] << endl;
  }
}
Problem p00750 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)

struct Edge {
  int v;
  string lab;
};

int n, a, s, g;
vector<vector<Edge>> adj;

string minimum_str = "{", spell[41][333];

void solve() {
  for_(i, 0, n) for_(len, 0, 333) spell[i][len] = minimum_str;

  spell[s][0] = "";

  for_(len, 0, 333) for_(v, 0, n) {
    if (spell[v][len] == minimum_str)
      continue;

    for (Edge e : adj[v]) {
      string add = e.lab;

      if (spell[e.v][len + add.size()] > spell[v][len] + add) {
        spell[e.v][len + add.size()] = spell[v][len] + add;
      }
    }
  }

  string ans = minimum_str;
  for_(len, 1, 333) ans = min(ans, spell[g][len]);

  if (ans == minimum_str || ans.size() > 240)
    cout << "NO" << endl;
  else
    cout << ans << endl;
}

int main() {
  while (cin >> n >> a >> s >> g, n) {
    adj.assign(n, vector<Edge>());

    for_(i, 0, a) {
      int x, y;
      string lab;
      cin >> x >> y >> lab;
      adj[x].push_back(Edge{y, lab});
    }

    solve();
  }
}

Error: MLE

Fixed submission:

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)

struct Edge {
  int v;
  string lab;
};

int n, a, s, g;
vector<vector<Edge>> adj;

string minimum_str = "{", spell[41][2411];

void solve() {
  for_(i, 0, n) for_(len, 0, 2411) spell[i][len] = minimum_str;

  spell[s][0] = "";

  for_(len, 0, 2411) for_(v, 0, n) {
    if (spell[v][len] == minimum_str)
      continue;

    string min_str = minimum_str;
    vector<Edge> use;

    for (Edge e : adj[v]) {
      if (min_str > e.lab) {
        min_str = e.lab;
        use.clear();
      }

      if (min_str == e.lab)
        use.push_back(e);
    }

    for (Edge e : use) {
      string add = e.lab;

      if (spell[e.v][len + add.size()] > spell[v][len] + add) {
        spell[e.v][len + add.size()] = spell[v][len] + add;
      }
    }
  }

  string ans = minimum_str;
  for_(len, 0, 2411) ans = min(ans, spell[g][len]);

  if (ans == minimum_str || ans.size() > 2400)
    cout << "NO" << endl;
  else
    cout << ans << endl;
}

int main() {
  while (cin >> n >> a >> s >> g, n) {
    adj.assign(n, vector<Edge>());

    for_(i, 0, a) {
      int x, y;
      string lab;
      cin >> x >> y >> lab;
      adj[x].push_back(Edge{y, lab});
    }

    solve();
  }
}
Problem p00750 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define N 50
#define M 500
using namespace std;
typedef pair<string, int> P;
vector<P> G[N];

string D[M][N];
string dijkstra(int S, int T) {

  for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++)
      D[i][j] = "{";

  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P("", S));
  D[0][S] = "";

  while (!Q.empty()) {
    P t = Q.top();
    Q.pop();
    string cost = t.first;
    int len = cost.size();
    int pos = t.second;

    if (len > M - 10)
      return "NO";
    if (pos == T)
      return cost;
    if (D[len][pos] < cost)
      continue;

    for (int i = 0; i < (int)G[pos].size(); i++) {
      int npos = G[pos][i].second;
      string ncost = cost + G[pos][i].first;
      int nlen = ncost.size();
      if (D[nlen][npos] <= ncost)
        continue;
      Q.push(P(ncost, npos));
      D[nlen][npos] = ncost;
    }
  }
  return "NO";
}

int main() {
  int cnt = 0;
  while (1) {
    cnt++;
    int n, m, s, g;
    cin >> n >> m >> s >> g;
    if (!n && !m && !s && !g)
      return 0;

    for (int i = 0; i < N; i++)
      G[i].clear();
    int D[N][N];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        D[i][j] = 1;

    for (int i = 0, a, b; i < m; i++) {
      string c;
      cin >> a >> b >> c;
      G[a].push_back(P(c, b));
      D[a][b] = 0;
    }

    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          D[i][j] = min(D[i][j], D[i][k] + D[k][j]);

    for (int i = 0; i < n; i++)
      if (D[i][g])
        G[i].clear();

    cout << dijkstra(s, g) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define N 40
#define M 1000
using namespace std;
typedef pair<string, int> P;
vector<P> G[N];

string D[M][N];
string dijkstra(int S, int T) {
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P("", S));
  for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++)
      D[i][j] = "{";

  D[0][0] = "";

  while (!Q.empty()) {
    P t = Q.top();
    Q.pop();
    string cost = t.first;
    int len = cost.size();
    int pos = t.second;

    if (len > M - 10)
      return "NO";
    if (D[len][pos] < cost)
      continue;
    if (pos == T)
      return cost;

    for (int i = 0; i < (int)G[pos].size(); i++) {
      int npos = G[pos][i].second;
      string ncost = cost + G[pos][i].first;
      int nlen = ncost.size();
      if (D[nlen][npos] <= ncost)
        continue;
      Q.push(P(ncost, npos));
      D[nlen][npos] = ncost;
    }
  }
  return "NO";
}

int main() {
  while (1) {
    int n, m, s, g;
    cin >> n >> m >> s >> g;
    if (!n && !m && !s && !g)
      return 0;

    for (int i = 0; i < N; i++)
      G[i].clear();

    for (int i = 0, a, b; i < m; i++) {
      string c;
      cin >> a >> b >> c;
      G[a].push_back(P(c, b));
    }

    cout << dijkstra(s, g) << endl;
  }

  return 0;
}
Problem p00750 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

struct edge {
  int src, dst;
  string cost;
  edge(int src, int dst, string cost) : src(src), dst(dst), cost(cost) {}
};
vector<edge> ve;
string d[42];
string INF = "{";
int n, s;
bool negcy = false;
int bellman_ford(int st) {
  rep(i, n + 1) d[i] = INF;
  d[st] = "";
  int cnt = 0;
  bool su = false;
  while (1) {
    bool update = false;
    rep(i, ve.size()) {
      edge m = ve[i];
      if (d[m.src] != INF && d[m.dst] > m.cost + d[m.src]) {
        d[m.dst] = m.cost + d[m.src];
        update = true;
        if (cnt >= n * 5 && m.dst == s)
          su = true;
      }
    }
    if (!update)
      break;
    if (cnt >= n * 7) {
      negcy = su;
      break;
    }
    ++cnt;
  }
}
int main() {
  int a, g;
  while (cin >> n >> a >> s >> g, n) {
    ve.clear();
    int sr, ds;
    string co;
    rep(i, a) {
      cin >> sr >> ds >> co;
      ve.pb(ds, sr, co);
    }
    negcy = false;
    bellman_ford(g);
    if (d[s] == INF || negcy)
      cout << "NO" << endl;
    else
      cout << d[s] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

struct edge {
  int src, dst;
  string cost;
  edge(int src, int dst, string cost) : src(src), dst(dst), cost(cost) {}
};
vector<edge> ve;
string d[42];
string INF = "{";
int n, s;
bool negcy = false;
int bellman_ford(int st) {
  rep(i, n + 1) d[i] = INF;
  d[st] = "";
  int cnt = 0;
  while (1) {
    bool update = false, su = false;
    rep(i, ve.size()) {
      edge m = ve[i];
      if (d[m.src] != INF && d[m.dst] > m.cost + d[m.src]) {
        d[m.dst] = m.cost + d[m.src];
        update = true;
        if (m.dst == s)
          su = true;
      }
    }
    if (!update)
      break;
    if (cnt >= ve.size() * 5) {
      negcy = su;
      break;
    }
    ++cnt;
  }
}
int main() {
  int a, g;
  while (cin >> n >> a >> s >> g, n) {
    ve.clear();
    int sr, ds;
    string co;
    rep(i, a) {
      cin >> sr >> ds >> co;
      ve.pb(ds, sr, co);
    }
    negcy = false;
    bellman_ford(g);
    if (d[s] == INF || negcy)
      cout << "NO" << endl;
    else
      cout << d[s] << endl;
  }
  return 0;
}
Problem p00750 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const string INF = "~";

class Edge {
public:
  int from, to;
  string cost;
  Edge(int f, int t, string c) : from(f), to(t), cost(c) {}
};

int n, a, s, g;
vector<Edge> edge;

string BellmanFord() {
  vector<string> memo(n, INF);
  memo[g] = "";
  for (int step = 0; step <= 6 * n; ++step) {
    for (const auto &e : edge) {
      int u = e.to;
      int v = e.from;
      string cost = e.cost;
      if (memo[u] == INF)
        continue;
      if (cost + memo[u] < memo[v]) {
        memo[v] = cost + memo[u];
        if (v == s && step >= n)
          return INF;
      }
    }
  }
  return memo[s];
}

int main() {
  while (cin >> n >> a >> s >> g, n | a | s | g) {
    edge.clear();
    for (int i = 0; i < a; ++i) {
      int x, y;
      string lab;
      cin >> x >> y >> lab;
      edge.push_back(Edge(x, y, lab));
    }
    string answer = BellmanFord();
    cout << (answer != INF ? answer : "NO") << endl;
  }
}

Error: MLE

Fixed submission:

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

const string NIL = "~";

int n, a, s, g;
string cost[40][40];

bool operator<(const string &s1, const string &s2) {
  for (int i = 0; i < min(s1.size(), s2.size()); ++i)
    if (s1[i] != s2[i])
      return s1[i] < s2[i];
  return s1.size() < s2.size();
}

void WarshalFloyd() {
  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        if (cost[i][k] != NIL && cost[k][j] != NIL)
          cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
}

int main() {
  while (cin >> n >> a >> s >> g, n | a | s | g) {
    for (int i = 0; i < 40; ++i)
      for (int j = 0; j < 40; ++j)
        cost[i][j] = NIL;
    for (int i = 0; i < a; ++i) {
      int x, y;
      string lab;
      cin >> x >> y >> lab;
      cost[x][y] = (cost[x][y] == NIL ? lab : min(cost[x][y], lab));
    }
    WarshalFloyd();
    string result1 = cost[s][g];
    WarshalFloyd();
    string result2 = cost[s][g];
    cout << (result1 != NIL && result1 == result2 ? result1 : "NO") << endl;
  }
}
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 += r.size() * 5;
    }

    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 / 5 || ans[s] == "")
      cout << "NO" << endl;
    else
      cout << ans[s] << endl;
  }
  return 0;
}