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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

struct poe {
  int src, dst;
  string lab;

public:
  poe(int src, int dst, string lab) : src(src), dst(dst), lab(lab) {}
  poe() : src(0), dst(0), lab("") {}
};

int main() {
  while (true) {
    int n, a, s, g;
    cin >> n >> a >> s >> g;
    if (n == 0)
      break;
    assert(n <= 40);
    assert(a <= 400);
    poe poes[a];
    REP(i, a) {
      int x, y;
      string lab;
      cin >> x >> y >> lab;
      // reverse
      poes[i] = poe(y, x, lab);
    }
    // bellman-ford
    string youngest[n];
    REP(i, n) youngest[i] = "~";
    youngest[g] = "";

    bool update = false;
    REP(i, n * 10) {
      update = false;
      REP(e, a) {
        poe poi = poes[e];
        int src = poi.src;
        int dst = poi.dst;
        if (youngest[src][0] == '~')
          continue;
        string nxt = poi.lab + youngest[src];
        if (youngest[dst] > nxt) {
          youngest[dst] = nxt;
          update = true;
          if (i >= n && dst == s) {
            update = false;
            youngest[s] = "~";
            break;
          }
        }
      }
      if (!update)
        break;
    }
    if (youngest[s][0] == '~') {
      cout << "NO" << endl;
    } else {
      cout << youngest[s] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

struct poe {
  int src, dst;
  string lab;

public:
  poe(int src, int dst, string lab) : src(src), dst(dst), lab(lab) {}
  poe() : src(0), dst(0), lab("") {}
};

int main() {
  while (true) {
    int n, a, s, g;
    cin >> n >> a >> s >> g;
    if (n == 0)
      break;
    assert(n <= 40);
    assert(a <= 400);
    poe poes[a];
    REP(i, a) {
      int x, y;
      string lab;
      cin >> x >> y >> lab;
      // reverse
      poes[i] = poe(y, x, lab);
    }
    // bellman-ford
    string youngest[n];
    REP(i, n) youngest[i] = "~";
    youngest[g] = "";

    bool update = false;
    bool ok = true;
    REP(i, n * a) {
      update = false;
      REP(e, a) {
        poe poi = poes[e];
        int src = poi.src;
        int dst = poi.dst;
        string nxt = poi.lab + youngest[src];
        if (youngest[dst] > nxt) {
          youngest[dst] = nxt;
          update = true;
          if (i >= n && dst == s) {
            youngest[s] = "~";
            ok = false;
            break;
          }
        }
      }
      if (!update)
        break;
    }
    if (!ok || youngest[s][0] == '~') {
      cout << "NO" << endl;
    } else {
      cout << youngest[s] << endl;
    }
  }
  return 0;
}