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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> pii;
static const int INF = 1000000000;
static int g_dist[256][102][100];

int main() {
  ios_base::sync_with_stdio(false);
  int n, m, e, s, t, r;
  cin >> n >> m >> e >> s >> t >> r;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
  }
  vector<int> x(e), y(e), z(e);
  for (int i = 0; i < e; ++i) {
    cin >> x[i] >> y[i] >> z[i];
  }
  vector<int> has_event(n, -1);
  for (int i = 0; i < e; ++i) {
    has_event[z[i]] = i;
  }
  for (int i = 0; i < (1 << e); ++i) {
    for (int j = 0; j <= r; ++j) {
      for (int k = 0; k < n; ++k) {
        g_dist[i][j][k] = INF;
      }
    }
  }
  const int init = (has_event[s] >= 0 ? (1 << has_event[s]) : 0);
  g_dist[init][0][s] = 0;
  int answer = INF;
  for (int i = 0; i < (1 << e); ++i) {
    vector<vector<int>> conn(n);
    for (int j = 0; j < m; ++j) {
      conn[a[j]].push_back(b[j]);
      conn[b[j]].push_back(a[j]);
    }
    for (int j = 0; j < e; ++j) {
      if (!(i & (1 << j))) {
        continue;
      }
      conn[x[j]].push_back(y[j]);
      conn[y[j]].push_back(x[j]);
    }
    for (int j = 0; j <= r; ++j) {
      for (int u = 0; u < n; ++u) {
        if (g_dist[i][j][u] >= INF) {
          continue;
        }
        g_dist[i][0][s] = min(g_dist[i][0][s], g_dist[i][j][u] + 1);
      }
    }
    for (int j = 0; j <= r; ++j) {
      for (int u = 0; u < n; ++u) {
        if (g_dist[i][j][u] >= INF) {
          continue;
        }
        if (has_event[u] >= 0) {
          const int mask = i | (1 << has_event[u]);
          g_dist[mask][j][u] = min(g_dist[mask][j][u], g_dist[i][j][u]);
        }
        for (int t = 0; t < conn[u].size(); ++t) {
          const int v = conn[u][t];
          g_dist[i][j + 1][v] = min(g_dist[i][j + 1][v], g_dist[i][j][u] + 1);
        }
      }
    }
    for (int j = 0; j <= r; ++j) {
      answer = min(answer, g_dist[i][j][t]);
    }
  }
  if (answer >= INF) {
    answer = -1;
  }
  cout << answer << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> pii;
static const int INF = 1000000000;
static int g_dist[256][102][100];

int main() {
  ios_base::sync_with_stdio(false);
  int n, m, e, s, t, r;
  cin >> n >> m >> e >> s >> t >> r;
  vector<int> a(m), b(m);
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
  }
  vector<int> x(m), y(m), z(m);
  for (int i = 0; i < e; ++i) {
    cin >> x[i] >> y[i] >> z[i];
  }
  vector<int> has_event(n, -1);
  for (int i = 0; i < e; ++i) {
    has_event[z[i]] = i;
  }
  for (int i = 0; i < (1 << e); ++i) {
    for (int j = 0; j <= r; ++j) {
      for (int k = 0; k < n; ++k) {
        g_dist[i][j][k] = INF;
      }
    }
  }
  const int init = (has_event[s] >= 0 ? (1 << has_event[s]) : 0);
  g_dist[init][0][s] = 0;
  int answer = INF;
  for (int i = 0; i < (1 << e); ++i) {
    vector<vector<int>> conn(n);
    for (int j = 0; j < m; ++j) {
      conn[a[j]].push_back(b[j]);
      conn[b[j]].push_back(a[j]);
    }
    for (int j = 0; j < e; ++j) {
      if (!(i & (1 << j))) {
        continue;
      }
      conn[x[j]].push_back(y[j]);
      conn[y[j]].push_back(x[j]);
    }
    for (int j = 0; j <= r; ++j) {
      for (int u = 0; u < n; ++u) {
        if (g_dist[i][j][u] >= INF) {
          continue;
        }
        g_dist[i][0][s] = min(g_dist[i][0][s], g_dist[i][j][u] + 1);
      }
    }
    for (int j = 0; j <= r; ++j) {
      for (int u = 0; u < n; ++u) {
        if (g_dist[i][j][u] >= INF) {
          continue;
        }
        if (has_event[u] >= 0) {
          const int mask = i | (1 << has_event[u]);
          g_dist[mask][j][u] = min(g_dist[mask][j][u], g_dist[i][j][u]);
        }
        for (int t = 0; t < conn[u].size(); ++t) {
          const int v = conn[u][t];
          g_dist[i][j + 1][v] = min(g_dist[i][j + 1][v], g_dist[i][j][u] + 1);
        }
      }
    }
    for (int j = 0; j <= r; ++j) {
      answer = min(answer, g_dist[i][j][t]);
    }
  }
  if (answer >= INF) {
    answer = -1;
  }
  cout << answer << endl;
  return 0;
}