Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

struct pair_hash {
  template <class T1, class T2>
  std::size_t operator()(const std::pair<T1, T2> &pair) const {
    return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
  }
};

// Graph template
using NodeId = pair<int, int>;

template <class... Args> struct Edge {
  NodeId from, to;
  tuple<Args...> values;
  Edge(NodeId from, NodeId to, Args... values)
      : from(from), to(to), values(values...) {}
};
template <class E> auto operator<(const E &e, const E &f) {
  return e.values > f.values;
}

template <class... Args> class Graph {
  using Edges = vector<Edge<Args...>>;
  unordered_map<NodeId, Edges, pair_hash> graph_;

public:
  // Graph(int n) : graph_(n) {}
  size_t size() { return graph_.size(); };
  size_t size() const { return graph_.size(); };
  auto &operator[](NodeId x) { return graph_[x]; }
  const auto &operator[](NodeId x) const { return graph_[x]; }
  void AddEdge(NodeId s, NodeId t, Args... v) {
    graph_[s].emplace_back(s, t, v...);
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  Graph<bool> g;
  for (int i = 0; i < m; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    g.AddEdge({u, c}, {v, c}, 0);
    g.AddEdge({v, c}, {u, c}, 0);
    g.AddEdge({u, 0}, {u, c}, 1);
    g.AddEdge({u, c}, {u, 0}, 0);
    g.AddEdge({v, 0}, {v, c}, 1);
    g.AddEdge({v, c}, {v, 0}, 0);
  }
  unordered_map<NodeId, int, pair_hash> used;
  deque<pair<NodeId, int>> dq;
  dq.push_front({{1, 0}, 0});
  while (!dq.empty()) {
    NodeId u;
    int cost;
    tie(u, cost) = dq.front();
    dq.pop_front();
    if (u == NodeId(n, 0)) {
      cout << cost << endl;
      return 0;
    }
    used[u] = 1;
    for (const auto &e : g[u]) {
      auto v = e.to;
      if (used[v])
        continue;
      auto weight = get<0>(e.values);
      if (weight) {
        dq.push_back({v, cost + 1});
      } else {
        dq.push_front({v, cost});
      }
    }
  }
  cout << -1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

struct pair_hash {
  template <class T1, class T2>
  std::size_t operator()(const std::pair<T1, T2> &pair) const {
    return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
  }
};

// Graph template
using NodeId = pair<int, int>;

template <class... Args> struct Edge {
  NodeId from, to;
  tuple<Args...> values;
  Edge(NodeId from, NodeId to, Args... values)
      : from(from), to(to), values(values...) {}
};
template <class E> auto operator<(const E &e, const E &f) {
  return e.values > f.values;
}

template <class... Args> class Graph {
  using Edges = vector<Edge<Args...>>;
  unordered_map<NodeId, Edges, pair_hash> graph_;

public:
  // Graph(int n) : graph_(n) {}
  size_t size() { return graph_.size(); };
  size_t size() const { return graph_.size(); };
  auto &operator[](NodeId x) { return graph_[x]; }
  const auto &operator[](NodeId x) const { return graph_[x]; }
  void AddEdge(NodeId s, NodeId t, Args... v) {
    graph_[s].emplace_back(s, t, v...);
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  Graph<bool> g;
  {
    unordered_map<NodeId, bool, pair_hash> maked;
    for (int i = 0; i < m; ++i) {
      int u, v, c;
      cin >> u >> v >> c;
      g.AddEdge({u, c}, {v, c}, 0);
      g.AddEdge({v, c}, {u, c}, 0);
      if (!maked[{u, c}]) {
        g.AddEdge({u, 0}, {u, c}, 1);
        g.AddEdge({u, c}, {u, 0}, 0);
      }
      if (!maked[{v, c}]) {
        g.AddEdge({v, 0}, {v, c}, 1);
        g.AddEdge({v, c}, {v, 0}, 0);
      }
      maked[{u, c}] = true;
      maked[{v, c}] = true;
    }
  }
  unordered_map<NodeId, int, pair_hash> used;
  deque<pair<NodeId, int>> dq;
  dq.push_front({{1, 0}, 0});
  while (!dq.empty()) {
    NodeId u;
    int cost;
    tie(u, cost) = dq.front();
    dq.pop_front();
    if (u == NodeId(n, 0)) {
      cout << cost << endl;
      return 0;
    }
    used[u] = 1;
    for (const auto &e : g[u]) {
      auto v = e.to;
      if (used[v])
        continue;
      auto weight = get<0>(e.values);
      if (weight) {
        dq.push_back({v, cost + 1});
      } else {
        dq.push_front({v, cost});
      }
    }
  }
  cout << -1 << endl;
}