Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

namespace std {
template <> class hash<std::vector<int>> {
public:
  size_t operator()(const std::vector<int> &x) const {
    size_t ret = 0;
    for (size_t i = 0; i < x.size(); i++)
      ret ^= hash<int>()(x[i]);
    return ret;
  }
};
} // namespace std

unordered_map<vector<int>, map<vector<int>, int>> G;

struct Edge {
  vector<int> v;
  int cost;
  Edge(vector<int> v, int cost) : v(v), cost(cost) {}
};
bool operator<(const Edge &a, const Edge &b) { return a.cost > b.cost; }

int dijkstra(const vector<int> &start, const vector<int> &goal) {
  unordered_map<vector<int>, int> trace;
  priority_queue<Edge, vector<Edge>> que;
  que.push(Edge(start, 0));
  while (!que.empty()) {
    Edge e = que.top();
    que.pop();
    vector<int> dest = {e.v[0], e.v[1]};
    if (trace.count(dest) > 0)
      continue;
    trace[dest] = 1;
    if (dest == goal)
      return e.cost;

    // cout << e[0] << " " << e[1] << "\t" << e[2] << endl;
    FOR(it, G[dest]) {
      vector<int> pos = it->first;
      int cst = it->second;
      if (trace.count(pos) == 0) {
        que.push(Edge({pos[0], pos[1]}, e.cost + cst));
      }
    }
  }
  return -1;
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<pair<int, int>> v[N];
  rep(i, M) {
    int p, q, c;
    cin >> p >> q >> c;
    G[{c, p}][{c, q}] = 0; // 駅を移動する
    G[{c, q}][{c, p}] = 0; // 駅を移動する

    G[{c, p}][{-1, p}] = 0; // 改札から出る
    G[{c, q}][{-1, q}] = 0; // 改札から出る
    G[{-1, p}][{c, p}] = 1; // 改札に入る
    G[{-1, q}][{c, q}] = 1; // 改札に入る
  }

  cout << dijkstra({-1, 1}, {-1, N}) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

namespace std {
template <> class hash<std::vector<int>> {
public:
  size_t operator()(const std::vector<int> &x) const {
    size_t ret = 0;
    for (size_t i = 0; i < x.size(); i++)
      ret ^= hash<int>()(x[i]);
    return ret;
  }
};
} // namespace std

map<vector<int>, map<vector<int>, int>> G;

struct Edge {
  vector<int> v;
  int cost;
  Edge(vector<int> v, int cost) : v(v), cost(cost) {}
};
bool operator<(const Edge &a, const Edge &b) { return a.cost > b.cost; }

int dijkstra(const vector<int> &start, const vector<int> &goal) {
  unordered_map<vector<int>, int> trace;
  priority_queue<Edge, vector<Edge>> que;
  que.push(Edge(start, 0));
  while (!que.empty()) {
    Edge e = que.top();
    que.pop();
    vector<int> dest = {e.v[0], e.v[1]};
    if (trace.count(dest) > 0)
      continue;
    trace[dest] = 1;
    if (dest == goal)
      return e.cost;

    // cout << e[0] << " " << e[1] << "\t" << e[2] << endl;
    FOR(it, G[dest]) {
      vector<int> pos = it->first;
      int cst = it->second;
      if (trace.count(pos) == 0) {
        que.push(Edge({pos[0], pos[1]}, e.cost + cst));
      }
    }
  }
  return -1;
}

int main() {
  int N, M;
  cin >> N >> M;

  vector<pair<int, int>> v[N];
  rep(i, M) {
    int p, q, c;
    cin >> p >> q >> c;
    G[{c, p}][{c, q}] = 0; // 駅を移動する
    G[{c, q}][{c, p}] = 0; // 駅を移動する

    G[{c, p}][{-1, p}] = 0; // 改札から出る
    G[{c, q}][{-1, q}] = 0; // 改札から出る
    G[{-1, p}][{c, p}] = 1; // 改札に入る
    G[{-1, q}][{c, q}] = 1; // 改札に入る
  }

  cout << dijkstra({-1, 1}, {-1, N}) << endl;

  return 0;
}