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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

struct edge {
  ll u, v, c;
  edge(ll u = 0, ll v = 0, ll c = 0) : u(u), v(v), c(c) {}
};
ll solve() {
  ll res = 0;
  ll N, M, X;
  cin >> N >> M >> X;
  vector<ll> T(N);
  for (auto &in : T)
    cin >> in;
  vector<vector<edge>> G(N);
  for (int i = 0; i < M; i++) {
    ll A, B, D;
    cin >> A >> B >> D;
    A--;
    B--;
    G[A].push_back(edge(A, B, D));
    G[B].push_back(edge(B, A, D));
  }
  vector<vector<vector<ll>>> dist(
      N, vector<vector<ll>>(3, vector<ll>(X + 1, LINF)));
  dist[0][0][0] = 0;
  using Items = tuple<ll, ll, ll>;
  queue<Items> q;
  q.push(Items(0, 0, 0));
  while (q.size()) {
    ll n, f, x;
    tie(n, f, x) = q.front();
    q.pop();
    for (auto e : G[n]) {
      if (x + e.c < X) {
        if (f == 0 && T[e.v] == 2)
          continue;
        if (f == 2 && T[e.v] == 0)
          continue;

        if (T[e.v] == 1) {
          if (dist[e.v][f][x + e.c] > dist[e.u][f][x] + e.c) {
            dist[e.v][f][x + e.c] = dist[e.u][f][x] + e.c;
            q.push(Items(e.v, f, x + e.c));
          }
        }
        if (T[e.v] == f) {
          if (dist[e.v][f][0] > dist[e.u][f][x] + e.c) {
            dist[e.v][f][0] = dist[e.u][f][x] + e.c;
            q.push(Items(e.v, f, 0));
          }
        }

      } else {
        if (T[e.v] == 1) {
          if (dist[e.v][T[e.v]][X] > dist[e.u][f][x] + e.c) {
            dist[e.v][T[e.v]][X] = dist[e.u][f][x] + e.c;
            q.push(Items(e.v, T[e.v], X));
          }
        } else {
          if (dist[e.v][T[e.v]][0] > dist[e.u][f][x] + e.c) {
            dist[e.v][T[e.v]][0] = dist[e.u][f][x] + e.c;
            q.push(Items(e.v, T[e.v], 0));
          }
        }
      }
    }
  }

  res = LINF;
  for (int i = 0; i <= X; i++) {
    res = min({res, dist[N - 1][0][i], dist[N - 1][1][i], dist[N - 1][2][i]});
  }
  return res;
}
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << solve() << endl;
  return 0;
}


Error: -6
free(): invalid next size (fast)


Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1)
      out << " ";
  }
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0)
      out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it))
      out << ", ";
  }
  out << " }";
  return out;
}

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

struct edge {
  ll u, v, c;
  edge(ll u = 0, ll v = 0, ll c = 0) : u(u), v(v), c(c) {}
};
ll solve() {
  ll res = 0;
  ll N, M, X;
  cin >> N >> M >> X;
  vector<ll> T(N);
  for (auto &in : T)
    cin >> in;
  vector<vector<edge>> G(N);
  for (int i = 0; i < M; i++) {
    ll A, B, D;
    cin >> A >> B >> D;
    A--;
    B--;
    G[A].push_back(edge(A, B, D));
    G[B].push_back(edge(B, A, D));
  }
  vector<vector<vector<ll>>> dist(
      N, vector<vector<ll>>(3, vector<ll>(X + 1, LINF)));
  dist[0][0][0] = 0;
  using Items = tuple<ll, ll, ll>;
  queue<Items> q;
  q.push(Items(0, 0, 0));
  while (q.size()) {
    ll n, s, x;
    tie(n, s, x) = q.front();
    q.pop();
    for (auto e : G[n]) {
      if (x + e.c < X) {
        if (s == 0 && T[e.v] == 2)
          continue;
        if (s == 2 && T[e.v] == 0)
          continue;

        if (T[e.v] == 1) {
          if (dist[e.v][s][x + e.c] > dist[e.u][s][x] + e.c) {
            dist[e.v][s][x + e.c] = dist[e.u][s][x] + e.c;
            q.push(Items(e.v, s, x + e.c));
          }
        }
        if (T[e.v] == s) {
          if (dist[e.v][s][x + e.c] > dist[e.u][s][x] + e.c) {
            dist[e.v][s][x + e.c] = dist[e.u][s][x] + e.c;
            q.push(Items(e.v, s, 0));
          }
        }
      } else {
        if (T[e.v] == 1) {
          if (dist[e.v][T[e.v]][X] > dist[e.u][s][x] + e.c) {
            dist[e.v][T[e.v]][x + e.c] = dist[e.u][s][x] + e.c;
            q.push(Items(e.v, T[e.v], X));
          }
        } else {
          if (dist[e.v][T[e.v]][0] > dist[e.u][s][x] + e.c) {
            dist[e.v][T[e.v]][0] = dist[e.u][s][x] + e.c;
            q.push(Items(e.v, T[e.v], 0));
          }
        }
      }
    }
  }

  res = LINF;
  for (int i = 0; i <= X; i++) {
    res = min({res, dist[N - 1][0][i], dist[N - 1][1][i], dist[N - 1][2][i]});
  }
  return res;
}
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << solve() << endl;
  return 0;
}