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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define INF (1 << 26)
struct edge {
  int to, cost, key;
};
struct state {
  int pos, pos2, S, cost;
  bool operator<(const state &p) const { return cost > p.cost; }
};

vector<edge> g[200];
int N, M, E, Si, Ti, R;
int c[200];
vector<int> nodes;
int dp[105][105];
int d[105][205][300];

void solve() {
  for (int i = 0; i < 105; i++)
    for (int j = 0; j < 205; j++)
      for (int k = 0; k < 300; k++)
        d[i][j][k] = INF;

  priority_queue<state> Q;
  int a = 0;
  if (c[Si] != -1)
    a = (1 << c[Si]);
  Q.push((state){Si, 0, a, 0});
  d[Si][0][a] = 0;

  while (!Q.empty()) {
    state s = Q.top();
    Q.pop();
    if (d[s.pos][s.pos2][s.S] < s.cost)
      continue;
    if (s.pos == Ti) {
      cout << s.cost << endl;
      return;
    }
    if (s.pos2 > R)
      continue;
    if (d[Si][0][s.S] > s.cost + 1) {
      d[Si][0][s.S] = s.cost + 1;
      Q.push((state){Si, 0, s.S, s.cost + 1});
    }

    for (int i = 0; i < (int)nodes.size(); i++) {
      int to = nodes[i];
      if (dp[s.pos][to] == INF)
        continue;
      int ncost = s.cost + dp[s.pos][to];
      int nS = s.S;
      int npos2 = s.pos + dp[s.pos][to];
      if (c[to] != -1)
        nS |= (1 << c[to]);
      if (d[to][npos2][nS] <= ncost)
        continue;
      d[to][npos2][nS] = ncost;
      Q.push((state){to, npos2, nS, ncost});
    }

    for (int i = 0; i < (int)g[s.pos].size(); i++) {
      edge e = g[s.pos][i];
      if (!((s.S >> e.key) & 1))
        continue;
      int to = e.to;
      int ncost = s.cost + e.cost;
      int nS = s.S;
      int npos2 = s.pos + e.cost;
      if (c[to] != -1)
        nS |= (1 << c[to]);
      if (d[to][npos2][nS] <= ncost)
        continue;
      d[to][npos2][nS] = ncost;
      Q.push((state){to, npos2, nS, ncost});
    }
  }
  cout << -1 << endl;
}

int main() {
  for (int i = 0; i < 105; i++) {
    c[i] = -1;
    for (int j = 0; j < 105; j++)
      dp[i][j] = (i == j ? 0 : INF);
  }

  cin >> N >> M >> E >> Si >> Ti >> R;
  int x, y, z;
  for (int i = 0; i < M; i++) {
    cin >> x >> y;
    dp[x][y] = dp[y][x] = min(dp[x][y], 1);
  }

  for (int i = 0; i < E; i++) {
    cin >> x >> y >> z;
    g[x].push_back((edge){y, 1, i});
    g[y].push_back((edge){x, 1, i});
    c[z] = i;
    nodes.push_back(x);
    nodes.push_back(y);
    nodes.push_back(z);
  }
  nodes.push_back(Si);
  nodes.push_back(Ti);
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);

  solve();
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define INF (1 << 26)
struct edge {
  int to, cost, key;
};
struct state {
  int pos, pos2, S, cost;
  bool operator<(const state &p) const { return cost > p.cost; }
};

vector<edge> g[200];
int N, M, E, Si, Ti, R;
int c[200];
vector<int> nodes;
int dp[105][105];
int d[105][205][300];

void solve() {
  for (int i = 0; i < 200; i++)
    for (int j = 0; j < 300; j++)
      for (int k = 0; k < 300; k++)
        d[i][j][k] = INF;

  priority_queue<state> Q;
  int a = 0;
  if (c[Si] != -1)
    a = (1 << c[Si]);
  Q.push((state){Si, 0, a, 0});
  d[Si][0][a] = 0;

  while (!Q.empty()) {
    state s = Q.top();
    Q.pop();
    if (d[s.pos][s.pos2][s.S] < s.cost)
      continue;
    if (s.pos == Ti) {
      cout << s.cost << endl;
      return;
    }
    if (s.pos2 > R)
      continue;
    if (d[Si][0][s.S] > s.cost + 1) {
      d[Si][0][s.S] = s.cost + 1;
      Q.push((state){Si, 0, s.S, s.cost + 1});
    }

    for (int i = 0; i < (int)nodes.size(); i++) {
      int to = nodes[i];
      if (dp[s.pos][to] == INF)
        continue;
      int ncost = s.cost + dp[s.pos][to];
      int nS = s.S;
      int npos2 = s.pos + dp[s.pos][to];
      if (c[to] != -1)
        nS |= (1 << c[to]);
      if (d[to][npos2][nS] <= ncost)
        continue;
      d[to][npos2][nS] = ncost;
      Q.push((state){to, npos2, nS, ncost});
    }

    for (int i = 0; i < (int)g[s.pos].size(); i++) {
      edge e = g[s.pos][i];
      if (!((s.S >> e.key) & 1))
        continue;
      int to = e.to;
      int ncost = s.cost + e.cost;
      int nS = s.S;
      int npos2 = s.pos + e.cost;
      if (c[to] != -1)
        nS |= (1 << c[to]);
      if (d[to][npos2][nS] <= ncost)
        continue;
      d[to][npos2][nS] = ncost;
      Q.push((state){to, npos2, nS, ncost});
    }
  }
  cout << -1 << endl;
}

int main() {
  for (int i = 0; i < 200; i++) {
    c[i] = -1;
    for (int j = 0; j < 200; j++)
      dp[i][j] = (i == j ? 0 : INF);
  }

  cin >> N >> M >> E >> Si >> Ti >> R;
  int x, y, z;
  for (int i = 0; i < M; i++) {
    cin >> x >> y;
    dp[x][y] = dp[y][x] = min(dp[x][y], 1);
  }

  for (int i = 0; i < E; i++) {
    cin >> x >> y >> z;
    g[x].push_back((edge){y, 1, i});
    g[y].push_back((edge){x, 1, i});
    c[z] = i;
    nodes.push_back(x);
    nodes.push_back(y);
    nodes.push_back(z);
  }
  nodes.push_back(Si);
  nodes.push_back(Ti);
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);

  solve();
  return 0;
}