Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MAXN 1000010
#define MAXM 1000010
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;

int read() {
  int res = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res;
}

struct Edge {
  int from, to, next, com;
  Edge() {}
  Edge(int u, int v, int n, int c) : from(u), to(v), next(n), com(c) {}
  bool operator<(const Edge &rhs) const { return com < rhs.com; }
};

struct Node {
  int d, u;
  bool operator<(const Node &rhs) const { return d > rhs.d; }
};

int n, m, cnt;
Edge edges[MAXM];
int head[MAXN], tot, vis[MAXN], dis[MAXN];
vector<int> comp[MAXN], G[MAXN];
vector<Edge> G1[MAXN];

void AddEdge(int u, int v, int com) {
  edges[tot] = Edge(u, v, head[u], com);
  head[u] = tot++;
  edges[tot] = Edge(v, u, head[v], com);
  head[v] = tot++;
}

set<int> dict;

void init() {
  tot = 0;
  for (int i = 1; i <= n; ++i)
    head[i] = -1;
}

void dijkstra(int s) {
  for (int i = 1; i <= n + cnt; ++i)
    dis[i] = INF, vis[i] = 0;
  priority_queue<Node> q;
  dis[s] = 0;
  q.push((Node){0, s});
  while (!q.empty()) {
    Node x = q.top();
    q.pop();
    int u = x.u;
    if (vis[u])
      continue;
    vis[u] = 1;
    int len = G[u].size();
    for (int i = 0; i != len; ++i) {
      int v = G[u][i];
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        q.push((Node){dis[v], v});
      }
    }
  }
}

int cur[MAXN];

int main() {
  freopen("03.txt", "r", stdin);
  n = read(), m = read();
  init();
  for (int i = 0; i != m; ++i) {
    int u, v, com;
    u = read(), v = read(), com = read();
    // buf[i].u=u,buf[i].v=v,buf[i].com=com;
    G1[u].push_back(Edge(u, v, -1, com));
    G1[v].push_back(Edge(v, u, -1, com));
    comp[com].push_back(u);
    dict.insert(com);
  }
  for (int i = 1; i <= n; ++i)
    sort(G1[i].begin(), G1[i].end());
  for (set<int>::iterator it = dict.begin(); it != dict.end(); ++it) {
    int com = *it;
    int len = comp[com].size();
    for (int i = 0; i != len; ++i) {
      int u = comp[com][i];
      if (vis[u] == com)
        continue;
      vis[u] = com;
      queue<int> q;
      q.push(u);
      ++cnt;
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        int len = G1[u].size();
        for (int &i = cur[u]; i != len; ++i) {
          Edge &e = G1[u][i];
          if (e.com != com)
            break;
          if (vis[e.to] != com) {
            vis[e.to] = com;
            q.push(e.to);
          }
        }
        G[u].push_back(n + cnt);
        G[n + cnt].push_back(u);
      }
    }
  }
  dijkstra(1);
  int ans;
  if (dis[n] == INF)
    ans = -1;
  else
    ans = dis[n] / 2;
  printf("%d\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define MAXN 1000010
#define MAXM 1000010
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;

int read() {
  int res = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    res = res * 10 + ch - '0';
    ch = getchar();
  }
  return res;
}

struct Edge {
  int from, to, next, com;
  Edge() {}
  Edge(int u, int v, int n, int c) : from(u), to(v), next(n), com(c) {}
  bool operator<(const Edge &rhs) const { return com < rhs.com; }
};

struct Node {
  int d, u;
  bool operator<(const Node &rhs) const { return d > rhs.d; }
};

int n, m, cnt;
Edge edges[MAXM];
int head[MAXN], tot, vis[MAXN], dis[MAXN];
vector<int> comp[MAXN], G[MAXN];
vector<Edge> G1[MAXN];

void AddEdge(int u, int v, int com) {
  edges[tot] = Edge(u, v, head[u], com);
  head[u] = tot++;
  edges[tot] = Edge(v, u, head[v], com);
  head[v] = tot++;
}

set<int> dict;

void init() {
  tot = 0;
  for (int i = 1; i <= n; ++i)
    head[i] = -1;
}

void dijkstra(int s) {
  for (int i = 1; i <= n + cnt; ++i)
    dis[i] = INF, vis[i] = 0;
  priority_queue<Node> q;
  dis[s] = 0;
  q.push((Node){0, s});
  while (!q.empty()) {
    Node x = q.top();
    q.pop();
    int u = x.u;
    if (vis[u])
      continue;
    vis[u] = 1;
    int len = G[u].size();
    for (int i = 0; i != len; ++i) {
      int v = G[u][i];
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        q.push((Node){dis[v], v});
      }
    }
  }
}

int cur[MAXN];

int main() {
  // freopen("03.txt","r",stdin);
  n = read(), m = read();
  init();
  for (int i = 0; i != m; ++i) {
    int u, v, com;
    u = read(), v = read(), com = read();
    // buf[i].u=u,buf[i].v=v,buf[i].com=com;
    G1[u].push_back(Edge(u, v, -1, com));
    G1[v].push_back(Edge(v, u, -1, com));
    comp[com].push_back(u);
    dict.insert(com);
  }
  for (int i = 1; i <= n; ++i)
    sort(G1[i].begin(), G1[i].end());
  for (set<int>::iterator it = dict.begin(); it != dict.end(); ++it) {
    int com = *it;
    int len = comp[com].size();
    for (int i = 0; i != len; ++i) {
      int u = comp[com][i];
      if (vis[u] == com)
        continue;
      vis[u] = com;
      queue<int> q;
      q.push(u);
      ++cnt;
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        int len = G1[u].size();
        for (int &i = cur[u]; i != len; ++i) {
          Edge &e = G1[u][i];
          if (e.com != com)
            break;
          if (vis[e.to] != com) {
            vis[e.to] = com;
            q.push(e.to);
          }
        }
        G[u].push_back(n + cnt);
        G[n + cnt].push_back(u);
      }
    }
  }
  dijkstra(1);
  int ans;
  if (dis[n] == INF)
    ans = -1;
  else
    ans = dis[n] / 2;
  printf("%d\n", ans);
}