Problem p04003 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i)                                                             \
  for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MMOD 998244353
#define MAX 2010101
using namespace std;
using Edge = pair<ll, ll>;
using Graph = vector<vector<Edge>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct edge {
  ll v, c, w;
};

int main() {
  map<ll, vector<edge>> ADJ[MAX];
  map<ll, ll> DIS[MAX];

  ll N, M;
  cin >> N >> M;
  REP(i, M) {
    ll U, V, C;
    cin >> U >> V >> C;

    ADJ[U][C].push_back({V, C, 0});
    ADJ[U][C].push_back({U, 0, 0});
    ADJ[U][0].push_back({U, C, 1});
    DIS[U][C] = MAX;

    ADJ[V][C].push_back({U, C, 0});
    ADJ[V][C].push_back({V, 0, 0});
    ADJ[V][0].push_back({V, C, 1});
    DIS[V][C] = MAX;
  }

  RREP(i, N) DIS[i][0] = MAX;

  deque<pair<ll, ll>> Q;
  Q.emplace_back(1, 0);
  DIS[1][0] = 0;

  while (!Q.empty()) {
    auto [u, c] = Q.front();
    Q.pop_front();

    for (auto e : ADJ[u][c]) {
      if (DIS[u][c] + e.w < DIS[e.v][e.c]) {
        DIS[e.v][e.c] = DIS[u][c] + e.w;
        if (e.w == 0)
          Q.emplace_front(e.v, e.c);
        else
          Q.emplace_back(e.v, e.c);
      }
    }
  }

  cout << (DIS[N][0] == MAX ? -1 : DIS[N][0]) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i)                                                             \
  for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MMOD 998244353
#define MAX 201010
using namespace std;
using Edge = pair<ll, ll>;
using Graph = vector<vector<Edge>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct edge {
  ll v, c, w;
};

int main() {
  map<ll, vector<edge>> ADJ[MAX];
  map<ll, ll> DIS[MAX];

  ll N, M;
  cin >> N >> M;
  REP(i, M) {
    ll U, V, C;
    cin >> U >> V >> C;

    ADJ[U][C].push_back({V, C, 0});
    ADJ[U][C].push_back({U, 0, 0});
    ADJ[U][0].push_back({U, C, 1});
    DIS[U][C] = MAX;

    ADJ[V][C].push_back({U, C, 0});
    ADJ[V][C].push_back({V, 0, 0});
    ADJ[V][0].push_back({V, C, 1});
    DIS[V][C] = MAX;
  }

  RREP(i, N) DIS[i][0] = MAX;

  deque<pair<ll, ll>> Q;
  Q.emplace_back(1, 0);
  DIS[1][0] = 0;

  while (!Q.empty()) {
    auto [u, c] = Q.front();
    Q.pop_front();

    for (auto e : ADJ[u][c]) {
      if (DIS[u][c] + e.w < DIS[e.v][e.c]) {
        DIS[e.v][e.c] = DIS[u][c] + e.w;
        if (e.w == 0)
          Q.emplace_front(e.v, e.c);
        else
          Q.emplace_back(e.v, e.c);
      }
    }
  }

  cout << (DIS[N][0] == MAX ? -1 : DIS[N][0]) << endl;
  return 0;
}