Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define PB push_back
#define SZ(x) (int)x.size()
#define MP make_pair
#define F first
#define S second
using namespace std;

typedef pair<int, int> pii;
typedef pair<pii, int> piii;

const int maxn = 1e5 + 5;

struct Edge {
  int from, to, type;
};
map<int, set<int>> G[maxn];

map<int, int> d[maxn];
int done[maxn], Mind[maxn];
int n;
const int INF = 0x3f3f3f3f;
int dijkstra(const int &st, const int &ed) {
  priority_queue<piii, vector<piii>, greater<piii>> pq;
  for (int i = 0; i < n; i++)
    d[i].clear(), done[i] = 0, Mind[i] = INF;
  pq.push(MP(MP(0, st), -1));
  Mind[st] = 0;
  while (!pq.empty()) {
    piii x = pq.top();
    pq.pop();
    int now = x.F.S, lasttype = x.S, noww = x.F.F;
    if (done[now] && noww > Mind[now])
      continue;
    done[now] = 1;

    for (const auto &it : G[now]) {
      int type = it.F;
      for (const int &to : it.S) {
        if (Mind[to] + 1 > noww + (lasttype == type ? 0 : 1) &&
            (!(d[to].count(type)) ||
             d[to][type] > noww + (lasttype == type ? 0 : 1))) {
          d[to][type] = noww + (lasttype == type ? 0 : 1);
          Mind[to] = min(Mind[to], noww + (lasttype == type ? 0 : 1));
          pq.push(MP(MP(noww + (lasttype == type ? 0 : 1), to), type));
        }
        if (type == lasttype)
          G[now][type].erase(to), G[to][type].erase(now);
      }
    }
  }
  int ans = INF;
  for (auto &&it : d[ed])
    ans = min(ans, it.S);
  return ans == INF ? -1 : ans;
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 0, a, b, c; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    G[a][c].insert(b);
    G[b][c].insert(a);
  }
  printf("%d\n", dijkstra(0, n - 1));
}


Fixed submission:
#include <bits/stdc++.h>
#define PB push_back
#define SZ(x) (int)x.size()
#define MP make_pair
#define F first
#define S second
using namespace std;

typedef pair<int, int> pii;
typedef pair<pii, int> piii;

const int maxn = 1e5 + 5;

struct Edge {
  int from, to, type;
};
map<int, set<int>> G[maxn];

map<int, int> d[maxn];
int done[maxn], Mind[maxn];
int n;
const int INF = 0x3f3f3f3f;
int dijkstra(const int &st, const int &ed) {
  priority_queue<piii, vector<piii>, greater<piii>> pq;
  for (int i = 0; i < n; i++)
    d[i].clear(), done[i] = 0, Mind[i] = INF;
  pq.push(MP(MP(0, st), -1));
  Mind[st] = 0;
  while (!pq.empty()) {
    piii x = pq.top();
    pq.pop();
    int now = x.F.S, lasttype = x.S, noww = x.F.F;
    for (const int &to : G[now][lasttype]) {
      int type = lasttype;
      if (Mind[to] + 1 > noww + (lasttype == type ? 0 : 1) &&
          (!(d[to].count(type)) ||
           d[to][type] > noww + (lasttype == type ? 0 : 1))) {
        d[to][type] = noww + (lasttype == type ? 0 : 1);
        Mind[to] = min(Mind[to], noww + (lasttype == type ? 0 : 1));
        pq.push(MP(MP(noww + (lasttype == type ? 0 : 1), to), type));
      }
      if (type == lasttype)
        G[now][type].erase(to), G[to][type].erase(now);
    }
    if (done[now])
      continue;
    done[now] = 1;

    for (const auto &it : G[now]) {
      int type = it.F;
      for (const int &to : it.S) {
        if (Mind[to] + 1 > noww + (lasttype == type ? 0 : 1) &&
            (!(d[to].count(type)) ||
             d[to][type] > noww + (lasttype == type ? 0 : 1))) {
          d[to][type] = noww + (lasttype == type ? 0 : 1);
          Mind[to] = min(Mind[to], noww + (lasttype == type ? 0 : 1));
          pq.push(MP(MP(noww + (lasttype == type ? 0 : 1), to), type));
        }
        if (type == lasttype)
          G[now][type].erase(to), G[to][type].erase(now);
      }
    }
  }
  int ans = INF;
  for (auto &&it : d[ed])
    ans = min(ans, it.S);
  return ans == INF ? -1 : ans;
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 0, a, b, c; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    G[a][c].insert(b);
    G[b][c].insert(a);
  }
  printf("%d\n", dijkstra(0, n - 1));
}