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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

struct edge {
  int from, to;
  string lab;

  edge() {}
  edge(int a, int b, string s) : from(a), to(b), lab(s) {}
};

int N, A, S, G;
vector<edge> edges;

string solve() {
  const string NO = "NO";
  vector<string> best(N, NO);
  best[G] = "";

  bool updated = true;
  vector<int> updated_at(N, 0);
  for (int j = 0; updated && j <= 7 * N; ++j) {
    updated = false;
    fill(begin(updated_at), end(updated_at), 0);

    for (const edge &e : edges) {
      if (best[e.to] == NO)
        continue;
      if (best[e.from] == NO || e.lab + best[e.to] < best[e.from]) {
        updated = true;
        updated_at[e.from] = true;
        best[e.from] = e.lab + best[e.to];
      }
    }
  }

  return best[S].size() <= 6 * (N - 1) ? best[S] : NO;
}

int main() {
  while (true) {
    cin >> N >> A >> S >> G;
    if (N == 0 && A == 0 && S == 0 && G == 0) {
      break;
    }
    edges.clear();
    for (int j = 0; j < A; ++j) {
      int x, y;
      string lab;
      cin >> x >> y >> lab;
      edges.emplace_back(x, y, lab);
    }
    cout << solve() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

struct edge {
  int from, to;
  string lab;

  edge() {}
  edge(int a, int b, string s) : from(a), to(b), lab(s) {}
};

int N, A, S, G;
vector<edge> edges;

string solve() {
  const string NO = "NO";
  vector<string> best(N, NO);
  best[G] = "";

  bool updated = true;
  vector<int> updated_at(N, 0);
  for (int j = 0; updated && j <= N; ++j) {
    updated = false;
    fill(begin(updated_at), end(updated_at), 0);

    for (const edge &e : edges) {
      if (best[e.to] == NO)
        continue;
      if (best[e.from] == NO ||
          (best[e.to] != NO && e.lab + best[e.to] < best[e.from])) {
        updated = true;
        updated_at[e.from] = true;
        best[e.from] = e.lab + best[e.to];
      }
    }
  }

  if (!updated) {
    return best[S];
  }

  vector<int> reachable(N, 0);
  reachable[S] = 1;
  for (int j = 0; updated; ++j) {
    updated = false;
    for (const edge &e : edges) {
      if (reachable[e.from]) {
        updated = 1;
        reachable[e.to] = 1;
        if (updated_at[e.to]) {
          return NO;
        }
      }
    }
  }

  return best[S];
}

int main() {
  while (true) {
    cin >> N >> A >> S >> G;
    if (N == 0 && A == 0 && S == 0 && G == 0) {
      break;
    }
    edges.clear();
    for (int j = 0; j < A; ++j) {
      int x, y;
      string lab;
      cin >> x >> y >> lab;
      edges.emplace_back(x, y, lab);
    }
    cout << solve() << endl;
  }
  return 0;
}