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

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

struct Edge {
  int to;
  string w;
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
const int SIZE = 500;
int n, a, s, g;
int vertex(int v, int length) { return length * n + v; }
int solve();
int main() {
  while (cin >> n >> a >> s >> g, n) {
    solve();
  }
}
int solve() {

  Graph graph(n * SIZE);
  for (int i = 0; i < a; i++) {
    int x, y;
    string lab;
    cin >> x >> y >> lab;
    for (int j = 0; j + lab.size() < SIZE; j++) {
      graph[vertex(x, j)].push_back(Edge{vertex(y, j + lab.size()), lab});
    }
  }
  string INF = "";
  for (int i = 0; i < 100; i++)
    INF += "z";

  vector<string> d(n * SIZE, INF);
  d[vertex(s, 0)] = "";
  for (int i = 0; i < n * SIZE; i++) {
    for (int j = 0; j < graph[i].size(); j++) {
      int to = graph[i][j].to;
      string tow = d[i] + graph[i][j].w;
      d[to] = min(d[to], tow);
    }
  }
  string res = INF;
  for (int i = 0; i < SIZE; i++) {
    if (i < SIZE / 2) {
      res = min(d[vertex(g, i)], res);
    } else {
      if (res > d[vertex(g, i)]) {
        cout << "NO" << endl;
        return 0;
      }
    }
  }
  cout << (res == INF ? "NO" : res) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

// define
#define ALL(a) a.begin(), a.end()
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define debug(x)                                                               \\
  if (1)                                                                       \\
    cout << #x << ":" << x << endl;
#define DEBUG(x)                                                               \\
  if (1)                                                                       \\
    cout << #x << ":" << x << endl;
#define ll long long

// constant
const int MOD = 1000000009;
const int INF = INT_MAX / 3;

// typedef
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

string dfs(int now, int n, int a, int s, int g, long long int been,
           const vector<int> &isgo, const vector<vector<string>> &edges,
           const vector<vector<int>> &edge) {
  if (now == g)
    return "";
  if ((been >> now) & (1LL))
    return "~";
  been = been | (1LL << now);
  string kouho = "~";
  string res = "~";
  if (edge[now].size())
    kouho = edges[now][edge[now][0]];
  for (int i = 0; i < edge[now].size(); i++) {
    res = min(res, dfs(edge[now][i], n, a, s, g, been, isgo, edges, edge));
  }

  return kouho + res;
}
int main() {
  int n, a, s, g;
  while (cin >> n >> a >> s >> g, n) {
    vector<vector<string>> edges(n, vector<string>(n, "~"));
    REP(i, a) {
      int x, y;
      string buf;
      cin >> x >> y >> buf;
      edges[x][y] = min(edges[x][y], buf);
    }

    vector<int> isgo(n, 0);
    isgo[g] = 1;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (isgo[j]) {
          for (int r = 0; r < n; r++)
            if (edges[r][j] != "~")
              isgo[r] = 1;
        }
    if (!isgo[s]) {
      cout << "NO" << endl;
      continue;
    }
    int now = s;
    vector<vector<int>> edge(n);
    for (int i = 0; i < n; i++) {
      if (!isgo[i])
        continue;
      string kouho = "~";
      for (int j = 0; j < n; j++)
        if (i != j && kouho > edges[i][j] && isgo[j])
          kouho = edges[i][j];
      if (kouho == "~")
        continue;
      if (kouho > edges[i][i])
        continue;
      for (int j = 0; j < n; j++)
        if (i != j && kouho == edges[i][j] && isgo[j])
          edge[i].push_back(j);
    }
    string res = dfs(now, n, a, s, g, 0, isgo, edges, edge);
    bool is = true;
    for (int i = 0; i < res.size(); i++)
      if (res[i] == '~') {
        is = false;
      }

    if (is)
      cout << res << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}