Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define iiii pair<int, pii>
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
#define rep(i, x) for (int(i) = 0; (i) < (x); (i)++)
inline int getint() {
  int x = 0, p = 1;
  char c = getchar();
  while (c <= 32)
    c = getchar();
  if (c == 45)
    p = -p, c = getchar();
  while (c > 32)
    x = x * 10 + c - 48, c = getchar();
  return x * p;
}
using namespace std;
//
const int maxn = 1e5 + 5;
vector<int> G[maxn];
int n, m, res = INF;
priority_queue<iiii, vector<iiii>, greater<iiii>> pq;
unordered_map<int, int> dis[maxn];
unordered_map<int, vector<int>> g[maxn];
//
iiii mp(int x, int y, int z) { return make_pair(x, make_pair(y, z)); }
int main() {
  n = getint();
  m = getint();
  rep(i, m) {
    int x = getint() - 1, y = getint() - 1, z = getint();
    G[x].push_back(z);
    G[y].push_back(z);
    g[x][z].push_back(y);
    g[y][z].push_back(x);
  }
  dis[0][0] = 0;
  pq.push(mp(0, 0, 0));
  while (!pq.empty()) {
    int len = pq.top().F;
    pii u = pq.top().S;
    pq.pop();
    if (dis[u.F][u.S] < len)
      continue;
    if (u.S != 0 && (dis[u.F].find(0) == dis[u.F].end() || dis[u.F][0] > len)) {
      dis[u.F][0] = len;
      pq.push(mp(len, u.F, 0));
    }
    if (!u.S) {
      for (int i : G[u.F])
        for (int to : g[u.F][i]) {
          if (dis[to].find(i) != dis[to].end() && dis[to][i] <= len + 1)
            continue;
          dis[to][i] = len + 1;
          pq.push(mp(len + 1, to, i));
        }
    } else {
      for (int to : g[u.F][u.S]) {
        if (dis[to].find(u.S) != dis[to].end() && dis[to][u.S] <= len)
          continue;
        dis[to][u.S] = len;
        pq.push(mp(len, to, u.S));
      }
    }
  }
  for (pii i : dis[n - 1])
    res = min(res, i.S);
  if (res == INF)
    res = -1;
  printf("%d\n", res);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int, int>
#define iiii pair<int, pii>
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
#define rep(i, x) for (int(i) = 0; (i) < (x); (i)++)
inline int getint() {
  int x = 0, p = 1;
  char c = getchar();
  while (c <= 32)
    c = getchar();
  if (c == 45)
    p = -p, c = getchar();
  while (c > 32)
    x = x * 10 + c - 48, c = getchar();
  return x * p;
}
using namespace std;
//
const int maxn = 1e5 + 5;
vector<int> G[maxn];
int n, m, res = INF;
priority_queue<iiii, vector<iiii>, greater<iiii>> pq;
unordered_map<int, int> dis[maxn];
unordered_map<int, vector<int>> g[maxn];
//
iiii mp(int x, int y, int z) { return make_pair(x, make_pair(y, z)); }
int main() {
  n = getint();
  m = getint();
  rep(i, m) {
    int x = getint() - 1, y = getint() - 1, z = getint();
    G[x].push_back(z);
    G[y].push_back(z);
    g[x][z].push_back(y);
    g[y][z].push_back(x);
  }
  dis[0][0] = 0;
  pq.push(mp(0, 0, 0));
  while (!pq.empty()) {
    int len = pq.top().F;
    pii u = pq.top().S;
    pq.pop();
    if (dis[u.F][u.S] < len)
      continue;
    if (u.S != 0 && (dis[u.F].find(0) == dis[u.F].end() || dis[u.F][0] > len)) {
      dis[u.F][0] = len;
      pq.push(mp(len, u.F, 0));
    }
    if (!u.S) {
      for (int i : G[u.F]) {
        if (dis[u.F].find(i) != dis[u.F].end() && dis[u.F][i] <= len + 1)
          continue;
        dis[u.F][i] = len + 1;
        pq.push(mp(len + 1, u.F, i));
      }
    } else {
      for (int to : g[u.F][u.S]) {
        if (dis[to].find(u.S) != dis[to].end() && dis[to][u.S] <= len)
          continue;
        dis[to][u.S] = len;
        pq.push(mp(len, to, u.S));
      }
    }
  }
  for (pii i : dis[n - 1])
    res = min(res, i.S);
  if (res == INF)
    res = -1;
  printf("%d\n", res);
  return 0;
}