Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define numa(x, a) for (auto x : a)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define pf push_front
#define sec second
#define sz(x) ((int)(x).size())
#define ALL(c) (c).begin(), (c).end()
#define gcd(a, b) __gcd(a, b)
#define mem(x, n) memset(x, n, sizeof(x))
#define endl "\n"
using namespace std;
template <int POS, class TUPLE>
void deploy(std::ostream &os, const TUPLE &tuple) {}
template <int POS, class TUPLE, class H, class... Ts>
void deploy(std::ostream &os, const TUPLE &t) {
  os << (POS == 0 ? "" : ", ") << get<POS>(t);
  deploy<POS + 1, TUPLE, Ts...>(os, t);
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, std::pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &v) {
  int remain = v.size();
  os << "{";
  for (auto e : v)
    os << e << (--remain == 0 ? "}" : ", ");
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, std::set<T> &v) {
  int remain = v.size();
  os << "{";
  for (auto e : v)
    os << e << (--remain == 0 ? "}" : ", ");
  return os;
}
template <class T, class K>
std::ostream &operator<<(std::ostream &os, std::map<T, K> &mp) {
  int remain = mp.size();
  os << "{";
  for (auto e : mp)
    os << "(" << e.first << " -> " << e.second << ")"
       << (--remain == 0 ? "}" : ", ");
  return os;
}
#define DEBUG1(var0)                                                           \
  { std::cerr << (#var0) << "=" << (var0) << endl; }
#define DEBUG2(var0, var1)                                                     \
  {                                                                            \
    std::cerr << (#var0) << "=" << (var0) << ", ";                             \
    DEBUG1(var1);                                                              \
  }
#define DEBUG3(var0, var1, var2)                                               \
  {                                                                            \
    std::cerr << (#var0) << "=" << (var0) << ", ";                             \
    DEBUG2(var1, var2);                                                        \
  }
#define DEBUG4(var0, var1, var2, var3)                                         \
  {                                                                            \
    std::cerr << (#var0) << "=" << (var0) << ", ";                             \
    DEBUG3(var1, var2, var3);                                                  \
  }
using ll = long long;

vector<vector<pair<pair<int, int>, int>>> edges[100100];
vector<pair<int, int>> inputs[1000100];
vector<int> cost[100100];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int N, M;
  cin >> N >> M;

  rep(i, M) {
    int x, y, c;
    cin >> x >> y >> c;
    x--;
    y--;
    inputs[c].pb(mp(x, y));
  }
  vector<pair<pair<int, int>, int>> empv;
  rep(i, 1000010) {
    if (sz(inputs[i]) > 0) {
      set<int> stat;
      numa(x, inputs[i]) {
        stat.insert(x.fir);
        stat.insert(x.sec);
      }
      numa(s, stat) { edges[s].push_back(empv); }
      numa(e, inputs[i]) {
        int x = e.fir, y = e.sec;
        int szx = sz(edges[x]);
        int szy = sz(edges[y]);
        edges[x][szx - 1].push_back(mp(mp(y, szy - 1), 0));
        edges[y][szy - 1].push_back(mp(mp(x, szx - 1), 0));
      }
    }
  }
  rep(j, 100010) {
    if (sz(edges[j]) > 0) {
      int n = sz(edges[j]);
      edges[j].push_back(empv);
      rep(i, n) {
        edges[j][i].push_back(mp(mp(j, n), 1));
        edges[j][n].push_back(mp(mp(j, i), 1));
      }
      cost[j].resize(n + 1, -1);
    }
  }
  if (sz(edges[0]) == 0) {
    cout << -1 << endl;
    return 0;
  }
  assert(sz(edges[0]) >= 2);
  assert(sz(edges[N - 1]) >= 2);
  pair<int, int> start = mp(0, sz(edges[0]) - 1);
  pair<int, int> goal = mp(N - 1, sz(edges[N - 1]) - 1);
  using T = pair<int, pair<int, int>>;
  priority_queue<T, vector<T>, greater<T>> que;
  que.push(mp(0, start));
  int ans = -1;
  while (!que.empty()) {
    auto t = que.top();
    que.pop();
    if (t.sec == goal) {
      ans = t.fir;
      break;
    }
    int c = t.fir;
    int x = t.sec.fir, y = t.sec.sec;
    // DEBUG3(x,y,c);
    if (cost[x][y] != -1) {
      continue;
    }
    cost[x][y] = c;
    numa(z, edges[x][y]) {
      int nx = z.fir.fir;
      int ny = z.fir.sec;
      int pc = z.sec;
      if (cost[nx][ny] == -1) {
        que.push(mp(c + pc, mp(nx, ny)));
      }
    }
  }
  if (ans == -1) {
    cout << -1 << endl;
  } else {
    cout << ans / 2 << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X) for (auto it = (X).begin(); it != (X).end(); it++)
#define numa(x, a) for (auto x : a)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define pf push_front
#define sec second
#define sz(x) ((int)(x).size())
#define ALL(c) (c).begin(), (c).end()
#define gcd(a, b) __gcd(a, b)
#define mem(x, n) memset(x, n, sizeof(x))
#define endl "\n"
using namespace std;
template <int POS, class TUPLE>
void deploy(std::ostream &os, const TUPLE &tuple) {}
template <int POS, class TUPLE, class H, class... Ts>
void deploy(std::ostream &os, const TUPLE &t) {
  os << (POS == 0 ? "" : ", ") << get<POS>(t);
  deploy<POS + 1, TUPLE, Ts...>(os, t);
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, std::pair<T, U> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &v) {
  int remain = v.size();
  os << "{";
  for (auto e : v)
    os << e << (--remain == 0 ? "}" : ", ");
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, std::set<T> &v) {
  int remain = v.size();
  os << "{";
  for (auto e : v)
    os << e << (--remain == 0 ? "}" : ", ");
  return os;
}
template <class T, class K>
std::ostream &operator<<(std::ostream &os, std::map<T, K> &mp) {
  int remain = mp.size();
  os << "{";
  for (auto e : mp)
    os << "(" << e.first << " -> " << e.second << ")"
       << (--remain == 0 ? "}" : ", ");
  return os;
}
#define DEBUG1(var0)                                                           \
  { std::cerr << (#var0) << "=" << (var0) << endl; }
#define DEBUG2(var0, var1)                                                     \
  {                                                                            \
    std::cerr << (#var0) << "=" << (var0) << ", ";                             \
    DEBUG1(var1);                                                              \
  }
#define DEBUG3(var0, var1, var2)                                               \
  {                                                                            \
    std::cerr << (#var0) << "=" << (var0) << ", ";                             \
    DEBUG2(var1, var2);                                                        \
  }
#define DEBUG4(var0, var1, var2, var3)                                         \
  {                                                                            \
    std::cerr << (#var0) << "=" << (var0) << ", ";                             \
    DEBUG3(var1, var2, var3);                                                  \
  }
using ll = long long;

vector<vector<pair<pair<int, int>, int>>> edges[100100];
vector<pair<int, int>> inputs[1000100];
vector<int> cost[100100];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int N, M;
  cin >> N >> M;

  rep(i, M) {
    int x, y, c;
    cin >> x >> y >> c;
    x--;
    y--;
    inputs[c].pb(mp(x, y));
  }
  vector<pair<pair<int, int>, int>> empv;
  rep(i, 1000010) {
    if (sz(inputs[i]) > 0) {
      set<int> stat;
      numa(x, inputs[i]) {
        stat.insert(x.fir);
        stat.insert(x.sec);
      }
      numa(s, stat) { edges[s].push_back(empv); }
      numa(e, inputs[i]) {
        int x = e.fir, y = e.sec;
        int szx = sz(edges[x]);
        int szy = sz(edges[y]);
        edges[x][szx - 1].push_back(mp(mp(y, szy - 1), 0));
        edges[y][szy - 1].push_back(mp(mp(x, szx - 1), 0));
      }
    }
  }
  rep(j, 100010) {
    if (sz(edges[j]) > 0) {
      int n = sz(edges[j]);
      edges[j].push_back(empv);
      rep(i, n) {
        edges[j][i].push_back(mp(mp(j, n), 1));
        edges[j][n].push_back(mp(mp(j, i), 1));
      }
      cost[j].resize(n + 1, -1);
    }
  }
  if (sz(edges[0]) == 0 || sz(edges[N - 1]) == 0) {
    cout << -1 << endl;
    return 0;
  }
  assert(sz(edges[0]) >= 2);
  assert(sz(edges[N - 1]) >= 2);
  pair<int, int> start = mp(0, sz(edges[0]) - 1);
  pair<int, int> goal = mp(N - 1, sz(edges[N - 1]) - 1);
  using T = pair<int, pair<int, int>>;
  priority_queue<T, vector<T>, greater<T>> que;
  que.push(mp(0, start));
  int ans = -1;
  while (!que.empty()) {
    auto t = que.top();
    que.pop();
    if (t.sec == goal) {
      ans = t.fir;
      break;
    }
    int c = t.fir;
    int x = t.sec.fir, y = t.sec.sec;
    // DEBUG3(x,y,c);
    if (cost[x][y] != -1) {
      continue;
    }
    cost[x][y] = c;
    numa(z, edges[x][y]) {
      int nx = z.fir.fir;
      int ny = z.fir.sec;
      int pc = z.sec;
      if (cost[nx][ny] == -1) {
        que.push(mp(c + pc, mp(nx, ny)));
      }
    }
  }
  if (ans == -1) {
    cout << -1 << endl;
  } else {
    cout << ans / 2 << endl;
  }
  return 0;
}