Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

typedef pair<int, int> pint;
map<pint, int> ma;
set<int> used;
int E;

vector<pint> G[110000];
vector<int> nG[510000];
int n, m;

int INF = 1 << 29;
int dp[510000];

int solve() {
  deque<pint> deq;
  for (int i = 0; i < 510000; ++i)
    dp[i] = INF;
  deq.push_back(pint(E, 0));
  dp[E] = 0;

  while (!deq.empty()) {
    int v = deq.front().first;
    int dist = deq.front().second;
    deq.pop_front();

    if (dp[v] < dist)
      continue;

    // cout << v << ", " << c << ": " << dp[ ma[ pint(v,c) ]] << endl;

    for (int i = 0; i < nG[v].size(); ++i) {
      int nv = nG[v][i];

      if (v < E && nv < E) {
        if (chmin(dp[nv], dp[v])) {
          deq.push_front(pint(nv, dp[nv]));
        }
      } else {
        if (chmin(dp[nv], dp[v] + 1)) {
          deq.push_front(pint(nv, dp[nv]));
        }
      }
    }
  }

  int res = dp[E + n - 1];
  if (res >= INF)
    return -1;
  else
    return res / 2;
}

int main() {
  while (cin >> n >> m) {
    for (int i = 0; i < m; ++i) {
      int p, q, c;
      scanf("%d %d %d", &p, &q, &c);
      --p, --q, --c;
      G[p].push_back(pint(q, c));
      G[q].push_back(pint(p, c));
      ma[pint(p, c)] = 0;
      ma[pint(q, c)] = 0;
    }
    int con = 0;
    for (map<pint, int>::iterator it = ma.begin(); it != ma.end(); ++it) {
      it->second = con++;
    }
    E = (int)ma.size();

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < G[i].size(); ++j) {
        int to = G[i][j].first;
        int c = G[i][j].second;

        int u = ma[pint(i, c)];
        int v = ma[pint(to, c)];

        nG[u].push_back(v);
        nG[v].push_back(u);

        if (!used.count(u)) {
          nG[u].push_back(E + i);
          nG[E + i].push_back(u);
          used.insert(u);
        }
      }
    }

    cout << solve() << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

typedef pair<int, int> pint;
map<pint, int> ma;
set<int> used;
int E;

vector<pint> G[110000];
vector<int> nG[510000];
int n, m;

int INF = 1 << 29;
int dp[510000];

int solve() {
  deque<pint> deq;
  for (int i = 0; i < 510000; ++i)
    dp[i] = INF;
  deq.push_back(pint(E, 0));
  dp[E] = 0;

  while (!deq.empty()) {
    int v = deq.front().first;
    int dist = deq.front().second;
    deq.pop_front();

    if (dp[v] < dist)
      continue;

    // cout << v << ", " << c << ": " << dp[ ma[ pint(v,c) ]] << endl;

    for (int i = 0; i < nG[v].size(); ++i) {
      int nv = nG[v][i];

      if (v < E && nv < E) {
        if (chmin(dp[nv], dp[v])) {
          deq.push_front(pint(nv, dp[nv]));
        }
      } else {
        if (chmin(dp[nv], dp[v] + 1)) {
          deq.push_back(pint(nv, dp[nv]));
        }
      }
    }
  }

  int res = dp[E + n - 1];
  if (res >= INF)
    return -1;
  else
    return res / 2;
}

int main() {
  while (cin >> n >> m) {
    for (int i = 0; i < m; ++i) {
      int p, q, c;
      scanf("%d %d %d", &p, &q, &c);
      --p, --q, --c;
      G[p].push_back(pint(q, c));
      G[q].push_back(pint(p, c));
      ma[pint(p, c)] = 0;
      ma[pint(q, c)] = 0;
    }
    int con = 0;
    for (map<pint, int>::iterator it = ma.begin(); it != ma.end(); ++it) {
      it->second = con++;
    }
    E = (int)ma.size();

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < G[i].size(); ++j) {
        int to = G[i][j].first;
        int c = G[i][j].second;

        int u = ma[pint(i, c)];
        int v = ma[pint(to, c)];

        nG[u].push_back(v);
        nG[v].push_back(u);

        if (!used.count(u)) {
          nG[u].push_back(E + i);
          nG[E + i].push_back(u);
          used.insert(u);
        }
      }
    }

    cout << solve() << endl;
  }
}