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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<vector<pii>> graph;

const int INF = 1e9;

inline int match(int cur, int in, const string &s) {
  string prv = s.substr(0, cur) + (char)(in + '0');
  rep(i, cur + 1) {
    int len = cur + 1 - i;
    if (s.substr(0, len) == prv.substr(i, len))
      return len;
  }
  return 0;
}

void dfs(int cur, vector<int> &vis, graph &g) {
  if (vis[cur])
    return;
  vis[cur] = 1;
  for (pii e : g[cur])
    dfs(e.second, vis, g);
}

bool BellmanFord(int s, int V, vector<int> &d, const graph &g) {
  d = vi(V, INF);
  d[s] = 0;
  rep(azu, V) {
    bool f = false;
    rep(i, V) for (pii e : g[i]) {
      if (d[i] < INF && d[e.second] > d[i] + e.first) {
        d[e.second] = d[i] + e.first;
        f = true;
      }
    }
    if (!f)
      return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, m, k, l;
  string pattern;
  while (cin >> n, n) {
    vi col(n);
    rep(i, n) cin >> col[i], col[i]--;
    cin >> m;
    graph base(n);
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      base[a].push_back(pii(c, b));
    }
    cin >> k >> pattern;
    l = pattern.size();

    vector<vi> trans(l, vi(4, 0));
    rep(i, l) rep(j, 4) { trans[i][j] = match(i, j + 1, pattern); }

    int V = n * l;
    graph g(V), rg(V);
    rep(i, l) rep(j, n) {
      for (pii e : base[j]) {
        int to = e.second, nxt = trans[i][col[to]];
        if (nxt == l)
          continue;
        g[i * n + j].push_back(pii(e.first, nxt * n + to));
        rg[nxt * n + to].push_back(pii(e.first, i * n + j));
      }
    }

    if (trans[0][col[0]] == l) {
      cout << "0 0" << endl;
      continue;
    }

    int S = trans[0][col[0]] * n;
    vi vis(V, 0), rvis(V, 0);
    dfs(S, vis, g);
    rep(i, l) dfs(i * n + n - 1, rvis, rg);

    rep(i, V) {
      if (vis[i] == 0 || rvis[i] == 0)
        g[i].clear();
      rep(j, g[i].size()) {
        int to = g[i][j].second;
        if (vis[to] == 0 || rvis[to] == 0) {
          g[i].erase(g[i].begin() + j);
          j--;
        }
      }
    }

    vi h;
    if (!BellmanFord(S, V, h, g)) {
      cout << -1 << endl;
      continue;
    }

    vector<vi> d(V, vi(k, INF));
    d[S][0] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> q;
    q.push(pii(0, S));

    while (q.size()) {
      pii p = q.top();
      q.pop();
      int v = p.second, c = p.first;

      for (pii e : g[v]) {
        int nv = e.second, nc = c + e.first + h[v] - h[nv];
        bool f = true;
        rep(i, k) {
          if (d[nv][i] > nc) {
            if (f)
              q.push(pii(nc, nv)), f = false;
            swap(d[nv][i], nc);
          }
        }
      }
    }

    vi can;
    rep(i, l) rep(j, k) {
      if (d[i * n + n - 1][j] != INF) {
        can.push_back(d[i * n + n - 1][j] - h[S] + h[i * n + n - 1]);
      }
    }
    sort(can.begin(), can.end());

    int ans = min((int)can.size(), k);
    long long sum = 0;
    rep(i, ans) sum += can[i];
    cout << ans << " " << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<vector<pii>> graph;

const int INF = 1e9;

inline int match(int cur, int in, const string &s) {
  string prv = s.substr(0, cur) + (char)(in + '0');
  rep(i, cur + 1) {
    int len = cur + 1 - i;
    if (s.substr(0, len) == prv.substr(i, len))
      return len;
  }
  return 0;
}

void dfs(int cur, vector<int> &vis, graph &g) {
  if (vis[cur])
    return;
  vis[cur] = 1;
  for (pii e : g[cur])
    dfs(e.second, vis, g);
}

bool BellmanFord(int s, int V, vector<int> &d, const graph &g) {
  d = vi(V, INF);
  d[s] = 0;
  rep(azu, V) {
    bool f = false;
    rep(i, V) for (pii e : g[i]) {
      if (d[i] < INF && d[e.second] > d[i] + e.first) {
        d[e.second] = d[i] + e.first;
        f = true;
      }
    }
    if (!f)
      return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, m, k, l;
  string pattern;
  while (cin >> n, n) {
    vi col(n);
    rep(i, n) cin >> col[i], col[i]--;
    cin >> m;
    graph base(n);
    rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      base[a].push_back(pii(c, b));
    }
    cin >> k >> pattern;
    l = pattern.size();

    vector<vi> trans(l, vi(4, -1));
    rep(i, l) rep(j, 4) { trans[i][j] = match(i, j + 1, pattern); }

    int V = n * l;
    graph g(V), rg(V);
    rep(i, l) rep(j, n) {
      for (pii e : base[j]) {
        int to = e.second, nxt = trans[i][col[to]];
        if (nxt == l)
          continue;
        g[i * n + j].push_back(pii(e.first, nxt * n + to));
        rg[nxt * n + to].push_back(pii(e.first, i * n + j));
      }
    }

    int S = trans[0][col[0]] * n;
    vi vis(V, 0), rvis(V, 0);
    dfs(S, vis, g);
    rep(i, l) dfs(i * n + n - 1, rvis, rg);

    rep(i, V) {
      if (vis[i] == 0 || rvis[i] == 0)
        g[i].clear();
      rep(j, g[i].size()) {
        int to = g[i][j].second;
        if (vis[to] == 0 || rvis[to] == 0) {
          g[i].erase(g[i].begin() + j);
          j--;
        }
      }
    }

    vi h;
    if (!BellmanFord(S, V, h, g)) {
      cout << -1 << endl;
      continue;
    }

    vector<vi> d(V, vi(k, INF));
    d[S][0] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> q;
    q.push(pii(0, S));

    while (q.size()) {
      pii p = q.top();
      q.pop();
      int v = p.second, c = p.first;

      for (pii e : g[v]) {
        int nv = e.second, nc = c + e.first + h[v] - h[nv];
        bool f = true;
        rep(i, k) {
          if (d[nv][i] > nc) {
            if (f)
              q.push(pii(nc, nv)), f = false;
            swap(d[nv][i], nc);
          }
        }
      }
    }

    vi can;
    rep(i, l) rep(j, k) {
      if (d[i * n + n - 1][j] != INF) {
        can.push_back(d[i * n + n - 1][j] - h[S] + h[i * n + n - 1]);
      }
    }
    sort(can.begin(), can.end());

    int ans = min((int)can.size(), k);
    long long sum = 0;
    rep(i, ans) sum += can[i];
    cout << ans << " " << sum << endl;
  }
}