Problem p00750 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

struct edge {
  int to;
  char c;
  edge() {}
  edge(int a, char b) { to = a, c = b; }
};

int n, m, s, g;
vector<edge> G[2505];
vector<int> revG[2505];
string ans;
bool valid[2505], nvalid[2505];
bool reach[2505];

void dfs(int v) {
  reach[v] = true;
  for (int i = 0; i < revG[v].size(); i++) {
    if (reach[revG[v][i]])
      continue;
    dfs(revG[v][i]);
  }
}

int main(void) {
  while (1) {
    cin >> n >> m >> s >> g;
    if (n == 0 && m == 0 && s == 0 && g == 0)
      break;
    s++, g++;
    for (int i = 1; i < 2505; i++)
      G[i].clear();

    int V = n, u, v;
    string lab;
    for (int i = 1; i <= m; i++) {
      cin >> u >> v >> lab;
      u++, v++;
      int pre = u;
      for (int j = 0; j < (int)lab.size() - 1; j++) {
        V++;
        G[pre].push_back(edge(V, lab[j]));
        pre = V;
      }
      G[pre].push_back(edge(v, lab[(int)lab.size() - 1]));
    }
    for (int i = 1; i <= V; i++)
      revG[i].clear();
    for (int i = 1; i <= V; i++) {
      for (int j = 0; j < G[i].size(); j++) {
        revG[G[i][j].to].push_back(i);
      }
    }
    for (int i = 1; i <= V; i++)
      reach[i] = false;
    dfs(g);

    ans = "";
    for (int i = 1; i <= V; i++)
      valid[i] = false;
    valid[s] = true;
    for (int i = 0; i < 305; i++) {

      if (valid[g]) {
        cout << ans << endl;
        goto end;
      }
      bool found = false;
      for (int j = 0; j < 26; j++) {
        bool flag = false;
        for (int k = 1; k <= V; k++) {
          if (!valid[k])
            continue;
          for (int l = 0; l < G[k].size(); l++) {
            if (G[k][l].c == j + 'a' && reach[G[k][l].to]) {
              flag = true;
              goto pass;
            }
          }
        }
      pass:;
        if (!flag)
          continue;
        for (int k = 1; k <= V; k++)
          nvalid[k] = false;
        for (int k = 1; k <= V; k++) {
          if (!valid[k])
            continue;
          for (int l = 0; l < G[k].size(); l++) {
            if (G[k][l].c == j + 'a' && reach[G[k][l].to])
              nvalid[G[k][l].to] = true;
          }
        }
        for (int k = 1; k <= V; k++)
          valid[k] = nvalid[k];
        found = true;
        ans += j + 'a';
        break;
      }
      if (!found) {
        cout << "NO" << endl;
        goto end;
      }
    }
    cout << "NO" << endl;
  end:;
  }
  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;

struct edge {
  int to;
  char c;
  edge() {}
  edge(int a, char b) { to = a, c = b; }
};

int n, m, s, g;
vector<edge> G[305];
string ans;
bool valid[305], nvalid[305];

int main(void) {
  while (1) {
    cin >> n >> m >> s >> g;
    if (n == 0 && m == 0 && s == 0 && g == 0)
      break;
    s++, g++;
    for (int i = 1; i < 305; i++)
      G[i].clear();

    int V = n, u, v;
    string lab;
    for (int i = 1; i <= m; i++) {
      cin >> u >> v >> lab;
      u++, v++;
      int pre = u;
      for (int j = 0; j < (int)lab.size() - 1; j++) {
        V++;
        G[pre].push_back(edge(V, lab[j]));
        pre = V;
      }
      G[pre].push_back(edge(v, lab[(int)lab.size() - 1]));
    }

    ans = "";
    for (int i = 1; i <= V; i++)
      valid[i] = false;
    valid[s] = true;
    for (int i = 0; i < 305; i++) {

      if (valid[g]) {
        cout << ans << endl;
        goto end;
      }
      bool found = false;
      for (int j = 0; j < 26; j++) {
        bool flag = false;
        for (int k = 1; k <= V; k++) {
          if (!valid[k])
            continue;
          for (int l = 0; l < G[k].size(); l++) {
            if (G[k][l].c == j + 'a') {
              flag = true;
              goto pass;
            }
          }
        }
      pass:;
        if (!flag)
          continue;
        for (int k = 1; k <= V; k++)
          nvalid[k] = false;
        for (int k = 1; k <= V; k++) {
          if (!valid[k])
            continue;
          for (int l = 0; l < G[k].size(); l++) {
            if (G[k][l].c == j + 'a')
              nvalid[G[k][l].to] = true;
          }
        }
        for (int k = 1; k <= V; k++)
          valid[k] = nvalid[k];
        found = true;
        ans += j + 'a';
        break;
      }
      if (!found) {
        cout << "NO" << endl;
        goto end;
      }
    }
    cout << "NO" << endl;
  end:;
  }
  return 0;
}