Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pa pair<int, int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read() {
  long long f = 1, sum = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
const int MAXN = 100010;
const int MAXM = 200010;
struct node {
  int x, col, cost;
  node() {}
  node(int _x, int _col, int _cost) : x(_x), col(_col), cost(_cost) {}
};
// bool operator <(const node a,const node b){return a.cost>b.cost;}
vector<int> g[MAXN];
map<int, vector<int>> e[MAXN];
map<int, int> dis[MAXN];
queue<node> q;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, col;
    scanf("%d%d%d", &u, &v, &col);
    e[u][col].push_back(v), e[v][col].push_back(u);
    g[col].push_back(u), g[col].push_back(v);
  }
  q.push(node(1, 0, 0));
  dis[1][0] = 0;
  while (!q.empty()) {
    node x = q.front();
    q.pop();
    //		cout<<x.x<<' '<<x.col<<' '<<x.cost<<endl;
    if (dis[x.x][x.col] != x.cost)
      continue;
    if (dis[x.x].find(0) == dis[x.x].end() || dis[x.x][0] > x.cost) {
      dis[x.x][0] = x.cost;
      q.push(node(x.x, 0, x.cost));
    }
    if (x.col == 0) {
      for (int i = 0; i < (int)g[x.x].size(); i++) {
        int ncol = g[x.x][i];
        if (dis[x.x].find(ncol) == dis[x.x].end() ||
            dis[x.x][ncol] > x.cost + 1) {
          dis[x.x][ncol] = x.cost + 1;
          q.push(node(x.x, ncol, x.cost + 1));
        }
      }
      continue;
    }
    for (int i = 0; i < (int)e[x.x][x.col].size(); i++) {
      int v = e[x.x][x.col][i];
      if (dis[v].find(x.col) == dis[v].end() || dis[v][x.col] > x.cost) {
        dis[v][x.col] = x.cost;
        q.push(node(v, x.col, x.cost));
      }
    }
  }
  int ans = INF;
  map<int, int>::iterator it;
  for (it = dis[n].begin(); it != dis[n].end(); it++)
    ans = min(ans, it->second);
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pa pair<int, int>
#define INF 0x3f3f3f3f
#define inf 0x3f
#define fi first
#define se second
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define pb push_back

using namespace std;

inline ll read() {
  long long f = 1, sum = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
const int MAXN = 100010;
const int MAXM = 200010;
struct node {
  int x, col, cost;
  node() {}
  node(int _x, int _col, int _cost) : x(_x), col(_col), cost(_cost) {}
};
// bool operator <(const node a,const node b){return a.cost>b.cost;}
vector<int> g[MAXN];
map<int, vector<int>> e[MAXN];
map<int, int> dis[MAXN];
queue<node> q;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, col;
    scanf("%d%d%d", &u, &v, &col);
    e[u][col].push_back(v), e[v][col].push_back(u);
    g[u].push_back(col), g[v].push_back(col);
  }
  q.push(node(1, 0, 0));
  dis[1][0] = 0;
  while (!q.empty()) {
    node x = q.front();
    q.pop();
    //		cout<<x.x<<' '<<x.col<<' '<<x.cost<<endl;
    if (dis[x.x][x.col] != x.cost)
      continue;
    if (dis[x.x].find(0) == dis[x.x].end() || dis[x.x][0] > x.cost) {
      dis[x.x][0] = x.cost;
      q.push(node(x.x, 0, x.cost));
    }
    if (x.col == 0) {
      for (int i = 0; i < (int)g[x.x].size(); i++) {
        int ncol = g[x.x][i];
        if (dis[x.x].find(ncol) == dis[x.x].end() ||
            dis[x.x][ncol] > x.cost + 1) {
          dis[x.x][ncol] = x.cost + 1;
          q.push(node(x.x, ncol, x.cost + 1));
        }
      }
      continue;
    }
    for (int i = 0; i < (int)e[x.x][x.col].size(); i++) {
      int v = e[x.x][x.col][i];
      if (dis[v].find(x.col) == dis[v].end() || dis[v][x.col] > x.cost) {
        dis[v][x.col] = x.cost;
        q.push(node(v, x.col, x.cost));
      }
    }
  }
  int ans = INF;
  map<int, int>::iterator it;
  for (it = dis[n].begin(); it != dis[n].end(); it++)
    ans = min(ans, it->second);
  if (ans == INF)
    cout << -1;
  else
    cout << ans;
  return 0;
}