Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
int n, m;
map<int, vector<int>> e[111111];
int len[111111];
bool trans[111111];
map<int, set<int>> a[111111];

int q[111111], op, cl;
int main() {
  memset(len, -1, sizeof(len));
  scanf("%d%d", &n, &m);
  while (m--) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    if (x > y)
      swap(x, y);
    e[x][z].push_back(y);
    e[y][z].push_back(x);
    if (x == 1) {
      len[y] = 1;
      a[1][z].insert(y);
    }
  }
  len[1] = 0;
  trans[1] = true;

  for (int step = 1;; step++) {

    if (a[step].empty()) {
      puts("-1");
      return 0;
    }

    // same color.
    for (map<int, set<int>>::iterator it = a[step].begin(); it != a[step].end();
         it++) {
      op = cl = 0;
      for (set<int>::iterator it2 = it->second.begin(); it2 != it->second.end();
           it2++)
        q[op++] = *it2;
      int col = it->first;
      while (cl < op) {
        int y = q[cl++];
        if (!e[y].count(col))
          continue;
        for (vector<int>::iterator it2 = e[y][col].begin();
             it2 != e[y][col].end(); it2++) {
          int to = *it2;
          if (len[to] != -1 && len[to] < step)
            continue;
          if (a[step][col].count(to))
            continue;
          a[step][col].insert(to);
          q[op++] = to;
          len[to] = step;
        }
      }
    }

    // different color
    for (map<int, set<int>>::iterator it = a[step].begin(); it != a[step].end();
         it++) {
      for (set<int>::iterator it2 = it->second.begin(); it2 != it->second.end();
           it2++) {
        int y = *it2;
        if (trans[y])
          continue;
        for (map<int, vector<int>>::iterator it3 = e[y].begin();
             it3 != e[y].end(); it3++)
          for (vector<int>::iterator it4 = it3->second.begin();
               it4 != it3->second.end(); it4++) {
            int to = *it4;
            if (len[to] != -1 && len[to] <= step)
              continue;
            len[to] = step + 1;
            a[step + 1][it3->first].insert(to);
          }
      }
    }

    if (len[n] != -1) {
      printf("%d\n", len[n]);
      return 0;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
int n, m;
map<int, vector<int>> e[111111];
int len[111111];
bool trans[111111];
map<int, set<int>> a[111111];

int q[111111], op, cl;
int main() {
  memset(len, -1, sizeof(len));
  scanf("%d%d", &n, &m);
  while (m--) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    if (x > y)
      swap(x, y);
    e[x][z].push_back(y);
    e[y][z].push_back(x);
    if (x == 1) {
      len[y] = 1;
      a[1][z].insert(y);
    }
  }
  len[1] = 0;
  trans[1] = true;

  for (int step = 1;; step++) {

    if (a[step].empty()) {
      puts("-1");
      return 0;
    }

    // same color.
    for (map<int, set<int>>::iterator it = a[step].begin(); it != a[step].end();
         it++) {
      op = cl = 0;
      for (set<int>::iterator it2 = it->second.begin(); it2 != it->second.end();
           it2++)
        q[op++] = *it2;
      int col = it->first;
      while (cl < op) {
        int y = q[cl++];
        if (!e[y].count(col))
          continue;
        for (vector<int>::iterator it2 = e[y][col].begin();
             it2 != e[y][col].end(); it2++) {
          int to = *it2;
          if (len[to] != -1 && len[to] < step)
            continue;
          if (a[step][col].count(to))
            continue;
          a[step][col].insert(to);
          q[op++] = to;
          len[to] = step;
        }
      }
    }

    // different color
    for (map<int, set<int>>::iterator it = a[step].begin(); it != a[step].end();
         it++) {
      for (set<int>::iterator it2 = it->second.begin(); it2 != it->second.end();
           it2++) {
        int y = *it2;
        if (trans[y])
          continue;
        for (map<int, vector<int>>::iterator it3 = e[y].begin();
             it3 != e[y].end(); it3++)
          for (vector<int>::iterator it4 = it3->second.begin();
               it4 != it3->second.end(); it4++) {
            int to = *it4;
            if (len[to] != -1 && len[to] <= step)
              continue;
            len[to] = step + 1;
            a[step + 1][it3->first].insert(to);
          }
        trans[y] = true;
      }
    }

    if (len[n] != -1) {
      printf("%d\n", len[n]);
      return 0;
    }
  }
}