Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <tuple>
#include <vector>

using namespace std;

typedef tuple<int, int, int> T;
typedef pair<int, int> P;
vector<T> Edges;
map<P, int> Stations; //(station,company)->sta_id
vector<P> E[400000];  // to,fee
bool isupdated[400000];
int dist[400000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    Stations[P(i, 0)] = 0;
  for (int i = 0; i < M; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    Edges.emplace_back(p, q, c);
    Stations[P(p, c)] = 0;
    Stations[P(q, c)] = 0;
  }
  int c = 0;
  for (auto &p : Stations)
    p.second = c++;
  fill(dist, dist + c, 100000000);
  for (auto &s : Stations) {
    auto p = s.first;
    int station = p.first, company = p.second, sta_id = s.second;
    if (company == 0)
      continue;
    E[Stations[P(station, 0)]].emplace_back(sta_id, 1);
    E[sta_id].emplace_back(Stations[P(station, 0)], 0);
  }
  for (auto &e : Edges) {
    int p = get<0>(e), q = get<1>(e), c = get<2>(e);
    int sp = Stations[P(p, c)], sq = Stations[P(q, c)];
    E[sp].emplace_back(sq, 0);
    E[sq].emplace_back(sp, 0);
  }

  deque<P> que; // cost,sta_id
  que.emplace_back(0, Stations[P(1, 0)]);
  dist[Stations[P(1, 0)]] = 0;
  int dest = Stations[P(N, 0)];
  while (!que.empty()) {
    P p = que.front();
    que.pop_front();
    int cost = p.first, sta_id = p.second;
    if (sta_id == dest) {
      cout << cost << endl;
      return 0;
    }
    if (isupdated[sta_id])
      continue;
    isupdated[sta_id] = true;

    for (auto &e : E[sta_id]) {
      int to = e.first, fee = e.second;
      if (cost + fee < dist[to]) {
        dist[to] = cost + fee;
        if (fee) {
          que.emplace_back(cost + fee, to);
        } else {
          que.emplace_front(cost + fee, to);
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <tuple>
#include <vector>

using namespace std;

typedef tuple<int, int, int> T;
typedef pair<int, int> P;
vector<T> Edges;
map<P, int> Stations; //(station,company)->sta_id
vector<P> E[500000];  // to,fee
bool isupdated[500000];
int dist[500000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    Stations[P(i, 0)] = 0;
  for (int i = 0; i < M; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    Edges.emplace_back(p, q, c);
    Stations[P(p, c)] = 0;
    Stations[P(q, c)] = 0;
  }
  int c = 0;
  for (auto &p : Stations)
    p.second = c++;
  fill(dist, dist + c, 100000000);
  for (auto &s : Stations) {
    auto p = s.first;
    int station = p.first, company = p.second, sta_id = s.second;
    if (company == 0)
      continue;
    E[Stations[P(station, 0)]].emplace_back(sta_id, 1);
    E[sta_id].emplace_back(Stations[P(station, 0)], 0);
  }
  for (auto &e : Edges) {
    int p = get<0>(e), q = get<1>(e), c = get<2>(e);
    int sp = Stations[P(p, c)], sq = Stations[P(q, c)];
    E[sp].emplace_back(sq, 0);
    E[sq].emplace_back(sp, 0);
  }

  deque<P> que; // cost,sta_id
  que.emplace_back(0, Stations[P(1, 0)]);
  dist[Stations[P(1, 0)]] = 0;
  int dest = Stations[P(N, 0)];
  while (!que.empty()) {
    P p = que.front();
    que.pop_front();
    int cost = p.first, sta_id = p.second;
    if (sta_id == dest) {
      cout << cost << endl;
      return 0;
    }
    if (isupdated[sta_id])
      continue;
    isupdated[sta_id] = true;

    for (auto &e : E[sta_id]) {
      int to = e.first, fee = e.second;
      if (cost + fee < dist[to]) {
        dist[to] = cost + fee;
        if (fee) {
          que.emplace_back(cost + fee, to);
        } else {
          que.emplace_front(cost + fee, to);
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}