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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

typedef char Weight;
struct Edge {
  int from, to;
  Weight cost;
  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
  g[from].push_back(Edge{from, to, cost});
}

int main() {
  int n, m, s, go;
  while (cin >> n >> m >> s >> go, n) {
    Graph g(n), rev(n);
    vector<tuple<int, int, string>> es;
    REP(i, m) {
      int a, b;
      cin >> a >> b;
      add_edge(rev, b, a, 0);
      string t;
      cin >> t;
      es.emplace_back(a, b, t);
    }
    vector<bool> vis(n);
    queue<int> que1;
    que1.push(go);
    vis[go] = true;
    while (que1.size()) {
      int t = que1.front();
      que1.pop();
      REP(i, rev[t].size()) {
        int nx = rev[t][i].to;
        if (!vis[nx]) {
          vis[nx] = true;
          que1.push(nx);
        }
      }
    }
    REP(i, m) {
      int a = get<0>(es[i]), b = get<1>(es[i]);
      string t = get<2>(es[i]);
      if (!(vis[a] && vis[b]) || a == go)
        continue;
      int tmp = a;
      REP(j, t.size() - 1) {
        g.resize(g.size() + 1);
        add_edge(g, tmp, g.size() - 1, t[j]);
        tmp = g.size() - 1;
      }
      add_edge(g, tmp, b, t.back());
    }

    string ans(2500, 'z');
    vs rec;
    REP(i, 3000) rec.push_back(string(i, 'z'));
    queue<pair<int, string>> que;
    que.push(make_pair(s, ""));
    bool f = false;
    int len = 0;
    while (que.size()) {
      int tmp = que.front().first;
      string s = que.front().second;

      if (len < s.size()) {
        set<pair<int, string>> st;
        int sz = que.size();
        REP(i, sz) {
          int tmp = que.front().first;
          string s = que.front().second;
          que.pop();
          if (s == rec[s.size()] && st.count(make_pair(tmp, s)) == 0) {
            st.insert(make_pair(tmp, s));
            que.emplace(tmp, s);
          }
        }
        len = s.size();
        continue;
      }

      que.pop();
      if (s > ans)
        continue;

      if (tmp == go) {
        chmin(ans, s);
        continue;
      }
      if (s.size() > 2500) {
        f = true;
        break;
      }
      REP(i, g[tmp].size()) {
        int nn = g[tmp][i].to;
        string ns = s + g[tmp][i].cost;
        if (ns > rec[ns.size()])
          continue;
        rec[ns.size()] = ns;
        que.push(make_pair(nn, ns));
      }
    }
    if (f || ans == string(2500, 'z'))
      puts("NO");
    else
      cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

typedef char Weight;
struct Edge {
  int from, to;
  Weight cost;
  bool operator<(const Edge &e) const { return cost < e.cost; }
  bool operator>(const Edge &e) const { return cost > e.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int from, int to, Weight cost) {
  g[from].push_back(Edge{from, to, cost});
}

int main() {
  int n, m, s, go;
  while (cin >> n >> m >> s >> go, n) {
    Graph g(n);
    REP(i, m) {
      int a, b;
      cin >> a >> b;
      string t;
      cin >> t;
      int tmp = a;
      REP(j, t.size() - 1) {
        g.resize(g.size() + 1);
        add_edge(g, tmp, g.size() - 1, t[j]);
        tmp = g.size() - 1;
      }
      add_edge(g, tmp, b, t.back());
    }
    string ans(2500, 'z');
    vs rec;
    REP(i, 3000) rec.push_back(string(i, 'z'));
    queue<pair<int, string>> que;
    que.push(make_pair(s, ""));
    bool f = false;
    int len = 0;
    while (que.size()) {
      int tmp = que.front().first;
      string s = que.front().second;

      if (len < s.size()) {
        int sz = que.size();
        REP(i, sz) {
          int tmp = que.front().first;
          string s = que.front().second;
          que.pop();
          if (s == rec[s.size()])
            que.emplace(tmp, s);
        }
        len = s.size();
        continue;
      }

      que.pop();
      if (s > ans)
        continue;

      if (tmp == go) {
        chmin(ans, s);
        continue;
      }
      if (s.size() > 2500) {
        f = true;
        break;
      }
      REP(i, g[tmp].size()) {
        int nn = g[tmp][i].to;
        string ns = s + g[tmp][i].cost;
        if (ns > rec[ns.size()])
          continue;
        rec[ns.size()] = ns;
        que.push(make_pair(nn, ns));
      }
    }
    if (f || ans == string(2500, 'z'))
      puts("NO");
    else
      cout << ans << endl;
  }
  return 0;
}