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

#include <bits/stdc++.h>

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif

using namespace std;

#define REP(i, a, b) for (int i = (a); i < int(b); ++i)
#define rep(i, n) REP(i, 0, n)
#define ALL(x) begin(x), end(x)

template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

struct edge {
  int to, need;
  edge(int to_, int need_) : to(to_), need(need_) {}
};

typedef pair<int, int> state;
typedef vector<vector<edge>> graph;
constexpr int ok = 0;
constexpr int INF = (1 << 29);

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

  int n, m, e, s, t, r;
  cin >> n >> m >> e >> s >> t >> r;

  vector<int> event(n, 0);
  graph G(n);

  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].emplace_back(b, ok);
    G[b].emplace_back(a, ok);
  }

  for (int i = 0; i < e; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    const int bit = 1 << i;
    event[c] = bit;
    G[a].emplace_back(b, bit);
    G[b].emplace_back(a, bit);
  }

  int ans = INF;
  vector<vector<int>> dist(n, vector<int>(1 << e, INF));
  vector<vector<int>> day(n, vector<int>(1 << e, INF));
  queue<state> que;

  dist[s][event[s]] = 0;
  day[s][event[s]] = 0;
  que.emplace(s, event[s]);

  for (int i = 0; i <= e; ++i) {
    vector<vector<bool>> used(n, vector<bool>(1 << e, false));

    while (!que.empty()) {
      const int v = que.front().first;
      const int bit = que.front().second;
      que.pop();

      if (day[v][bit] >= r) {
        while (!que.empty())
          que.pop();
        break;
      }

      if (used[v][bit])
        continue;
      used[v][bit] = true;

      for (const auto &E : G[v]) {
        if (E.need == ok || (E.need & bit)) {
          const int next_bit = bit | event[E.to];
          if (dist[E.to][next_bit] > dist[v][bit] + 1) {
            dist[E.to][next_bit] = dist[v][bit] + 1;
            day[E.to][next_bit] = day[v][bit] + 1;
            que.emplace(E.to, next_bit);
          }
        }
      }
    }

    for (int bit = 0; bit < (1 << e); ++bit) {
      chmin(ans, dist[t][bit] + i);

      bool update = false;
      for (int v = 0; v < n; ++v) {
        if (v == s)
          continue;

        if (dist[s][bit] > dist[v][bit]) {
          dist[s][bit] = dist[v][bit];
          update = true;
        }
        dist[v][bit] = INF;
      }

      if (update) {
        day[s][bit] = 0;
        que.emplace(s, bit);
      }
    }
  }

  cout << (ans == INF ? -1 : ans) << endl;

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif

using namespace std;

#define REP(i, a, b) for (int i = (a); i < int(b); ++i)
#define rep(i, n) REP(i, 0, n)
#define ALL(x) begin(x), end(x)

template <class T> inline void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

struct edge {
  int to, need;
  edge(int to_, int need_) : to(to_), need(need_) {}
};

typedef pair<int, int> state;
typedef vector<vector<edge>> graph;
constexpr int ok = 0;
constexpr int INF = (1 << 29);

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

  int n, m, e, s, t, r;
  cin >> n >> m >> e >> s >> t >> r;

  vector<int> event(n, 0);
  graph G(n);

  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].emplace_back(b, ok);
    G[b].emplace_back(a, ok);
  }

  for (int i = 0; i < e; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    const int bit = 1 << i;
    event[c] = bit;
    G[a].emplace_back(b, bit);
    G[b].emplace_back(a, bit);
  }

  int ans = INF;
  vector<vector<int>> dist(n, vector<int>(1 << e, INF));
  vector<vector<int>> day(n, vector<int>(1 << e, INF));
  queue<state> que;

  dist[s][event[s]] = 0;
  day[s][event[s]] = 0;
  que.emplace(s, event[s]);

  for (int i = 0; i <= e; ++i) {
    vector<vector<bool>> used(n, vector<bool>(1 << e, false));

    while (!que.empty()) {
      const int v = que.front().first;
      const int bit = que.front().second;
      que.pop();

      if (day[v][bit] >= r)
        continue;
      assert(!used[v][bit]);
      used[v][bit] = true;

      for (const auto &E : G[v]) {
        if (E.need == ok || (E.need & bit)) {
          const int next_bit = bit | event[E.to];
          if (dist[E.to][next_bit] > dist[v][bit] + 1) {
            dist[E.to][next_bit] = dist[v][bit] + 1;
            day[E.to][next_bit] = day[v][bit] + 1;
            que.emplace(E.to, next_bit);
          }
        }
      }
    }

    for (int bit = 0; bit < (1 << e); ++bit) {
      chmin(ans, dist[t][bit] + i);

      bool update = false;
      for (int v = 0; v < n; ++v) {
        if (v == s)
          continue;

        if (dist[s][bit] > dist[v][bit]) {
          dist[s][bit] = dist[v][bit];
          update = true;
        }
        dist[v][bit] = INF;
      }

      if (update) {
        day[s][bit] = 0;
        que.emplace(s, bit);
      }
    }
  }

  cout << (ans == INF ? -1 : ans) << endl;

  return EXIT_SUCCESS;
}