Problem p04003 (C++)

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

using namespace std;

const int INF = 1 << 30;

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  map<pair<int, int>, vector<pair<pair<int, int>, int>>> G;
  for (int i = 0; i < M; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    p--, q--;
    G[make_pair(p, c)].emplace_back(make_pair(make_pair(q, c), 0));
    G[make_pair(q, c)].emplace_back(make_pair(make_pair(p, c), 0));
    G[make_pair(p, c)].emplace_back(make_pair(make_pair(q, 0), 0));
    G[make_pair(q, c)].emplace_back(make_pair(make_pair(p, 0), 0));
    G[make_pair(p, 0)].emplace_back(make_pair(make_pair(q, c), 1));
    G[make_pair(q, 0)].emplace_back(make_pair(make_pair(p, c), 1));
  }
  set<pair<int, int>> used;
  vector<int> dist(N, INF);
  dist[0] = 0;
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      que;
  que.push(make_pair(0, make_pair(0, 0)));
  while (!que.empty()) {
    auto q = que.top();
    que.pop();
    int d = q.first, u = q.second.first, a = q.second.second;
    used.insert(make_pair(u, a));
    for (auto g : G[make_pair(u, a)]) {
      int v = g.first.first, b = g.first.second, c = g.second;
      if (used.find(make_pair(v, b)) != used.end())
        continue;
      que.push(make_pair(d + c, make_pair(v, b)));
      chmin(dist[v], d + c);
    }
  }
  if (dist[N - 1] != INF) {
    cout << dist[N - 1] << '\n';
  } else {
    cout << -1 << '\n';
  }
  return 0;
}


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

using namespace std;

const int INF = 1 << 30;

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  map<pair<int, int>, vector<pair<pair<int, int>, int>>> G;
  for (int i = 0; i < M; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    p--, q--;
    G[make_pair(p, c)].emplace_back(make_pair(make_pair(q, c), 0));
    G[make_pair(q, c)].emplace_back(make_pair(make_pair(p, c), 0));
    G[make_pair(p, c)].emplace_back(make_pair(make_pair(q, 0), 0));
    G[make_pair(q, c)].emplace_back(make_pair(make_pair(p, 0), 0));
    G[make_pair(p, 0)].emplace_back(make_pair(make_pair(q, c), 1));
    G[make_pair(q, 0)].emplace_back(make_pair(make_pair(p, c), 1));
  }
  set<pair<int, int>> used;
  vector<int> dist(N, INF);
  dist[0] = 0;
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      que;
  que.push(make_pair(0, make_pair(0, 0)));
  while (!que.empty()) {
    auto q = que.top();
    que.pop();
    int d = q.first, u = q.second.first, a = q.second.second;
    if (used.find(make_pair(u, a)) != used.end())
      continue;
    used.insert(make_pair(u, a));
    for (auto g : G[make_pair(u, a)]) {
      int v = g.first.first, b = g.first.second, c = g.second;
      if (used.find(make_pair(v, b)) != used.end())
        continue;
      que.push(make_pair(d + c, make_pair(v, b)));
      chmin(dist[v], d + c);
    }
  }
  if (dist[N - 1] != INF) {
    cout << dist[N - 1] << '\n';
  } else {
    cout << -1 << '\n';
  }
  return 0;
}