prithivMLmods/Coder-Stat huggingface dataset:
Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;

std::vector<std::map<int, std::vector<std::pair<std::pair<int, int>, int>>>> g;
std::vector<std::set<int>> st;
int n, m;

int main() {
  cin >> n >> m;
  g.resize(n);
  st.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;

    g[a - 1][c].push_back({{b - 1, c}, 0});
    g[b - 1][c].push_back({{a - 1, c}, 0});
    st[a - 1].insert(c);
    st[b - 1].insert(c);
  }

  for (int i = 0; i < n; i++) {
    for (auto s : st[i]) {
      g[i][s].push_back({{i, -1}, 0});
      g[i][-1].push_back({{i, s}, 1});
    }
  }

  std::priority_queue<std::pair<int, std::pair<int, int>>> qu;
  qu.push({0, {0, -1}});
  std::vector<std::map<int, int>> min_cost(n, std::map<int, int>());
  min_cost[0][-1] = 0;
  while (!qu.empty()) {
    auto p = qu.top();
    qu.pop();

    for (auto e : g[p.second.first][p.second.second]) {
      if (min_cost[e.first.first][e.first.second] and
          p.first + e.second >= min_cost[e.first.first][e.first.second])
        continue;
      min_cost[e.first.first][e.first.second] = p.first + e.second;
      qu.push({p.first + e.second, e.first});
    }
  }

  cout << (min_cost[n - 1][-1] ? min_cost[n - 1][-1] : -1) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;

std::vector<std::map<int, std::vector<std::pair<std::pair<int, int>, int>>>> g;
std::vector<std::set<int>> st;
int n, m;

int main() {
  cin >> n >> m;
  g.resize(n);
  st.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;

    g[a - 1][c].push_back({{b - 1, c}, 0});
    g[b - 1][c].push_back({{a - 1, c}, 0});
    st[a - 1].insert(c);
    st[b - 1].insert(c);
  }

  for (int i = 0; i < n; i++) {
    for (auto s : st[i]) {
      g[i][s].push_back({{i, -1}, 0});
      g[i][-1].push_back({{i, s}, 1});
    }
  }

  typedef std::pair<int, std::pair<int, int>> P;
  std::priority_queue<P, std::vector<P>, std::greater<P>> qu;
  qu.push({0, {0, -1}});
  std::vector<std::map<int, int>> min_cost(n, std::map<int, int>());
  min_cost[0][-1] = 0;
  while (!qu.empty()) {
    auto p = qu.top();
    qu.pop();

    for (auto e : g[p.second.first][p.second.second]) {
      if (min_cost[e.first.first][e.first.second] and
          p.first + e.second >= min_cost[e.first.first][e.first.second])
        continue;
      min_cost[e.first.first][e.first.second] = p.first + e.second;
      qu.push({p.first + e.second, e.first});
    }
  }

  cout << (min_cost[n - 1][-1] ? min_cost[n - 1][-1] : -1) << endl;
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, m;

const int maxn = 1e5 + 5;

unordered_map<int, vector<int>> e[maxn];
set<int> g[maxn]; // 颜色
unordered_map<int, int> d[maxn];

struct st {
  int p, b, c; // 点 颜色 花费
  st(int x, int y, int z) {
    p = x;
    b = y;
    c = z;
  }
};
bool operator<(const st &a, const st &b) { return a.c > b.c; }
priority_queue<st> q;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    e[u][c].push_back(v);
    e[v][c].push_back(c);
    g[u].insert(c);
    g[v].insert(c);
  }
  q.push(st(1, 0, 0));
  d[1][0] = 0;
  while (!q.empty()) {
    st tmp = q.top();
    q.pop();
    if (d[tmp.p][tmp.b] != tmp.c)
      continue;
    if (!d[tmp.p].count(0) || d[tmp.p][0] > tmp.c) {
      d[tmp.p][0] = tmp.c;
      q.push(st(tmp.p, 0, tmp.c));
    }
    if (tmp.b == 0) {
      for (auto v : g[tmp.p]) {
        if (!d[tmp.p].count(v) || d[tmp.p][v] > tmp.c + 1) {
          d[tmp.p][v] = tmp.c + 1;
          q.push(st(tmp.p, v, tmp.c + 1));
        }
      }
    } else {
      for (auto v : e[tmp.p][tmp.b]) {
        if (!d[v].count(tmp.b) || d[v][tmp.b] > tmp.c) {
          d[v][tmp.b] = tmp.c;
          q.push(st(v, tmp.b, tmp.c));
        }
      }
    }
  }
  int ans = INT_MAX;
  for (auto i : d[n]) {
    ans = min(ans, i.second);
  }
  cout << (ans == INT_MAX ? -1 : ans) << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, m;

const int maxn = 1e5 + 5;

unordered_map<int, vector<int>> e[maxn];
set<int> g[maxn]; // 颜色
unordered_map<int, int> d[maxn];

struct st {
  int p, b, c; // 点 颜色 花费
  st(int x, int y, int z) {
    p = x;
    b = y;
    c = z;
  }
};
bool operator<(const st &a, const st &b) { return a.c > b.c; }
priority_queue<st> q;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    e[u][c].push_back(v);
    e[v][c].push_back(u);
    g[u].insert(c);
    g[v].insert(c);
  }
  q.push(st(1, 0, 0));
  d[1][0] = 0;
  while (!q.empty()) {
    st tmp = q.top();
    q.pop();
    if (d[tmp.p][tmp.b] != tmp.c)
      continue;
    if (!d[tmp.p].count(0) || d[tmp.p][0] > tmp.c) {
      d[tmp.p][0] = tmp.c;
      q.push(st(tmp.p, 0, tmp.c));
    }
    if (tmp.b == 0) {
      for (auto v : g[tmp.p]) {
        if (!d[tmp.p].count(v) || d[tmp.p][v] > tmp.c + 1) {
          d[tmp.p][v] = tmp.c + 1;
          q.push(st(tmp.p, v, tmp.c + 1));
        }
      }
    } else {
      for (auto v : e[tmp.p][tmp.b]) {
        if (!d[v].count(tmp.b) || d[v][tmp.b] > tmp.c) {
          d[v][tmp.b] = tmp.c;
          q.push(st(v, tmp.b, tmp.c));
        }
      }
    }
  }
  int ans = INT_MAX;
  for (auto i : d[n]) {
    ans = min(ans, i.second);
  }
  cout << (ans == INT_MAX ? -1 : ans) << '\n';

  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define maxn 200005
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int> PII;
int n, m, cnt, dist[maxn], fa[maxn];
vector<int> G[maxn];
queue<int> q;
map<int, int> id[maxn];
int find(int x) {
  if (fa[x] == x)
    return x;
  return fa[x] = find(fa[x]);
}
void bfs() {
  dist[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = find(G[u][i]);
      if (dist[v] < 0) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i < maxn; i++)
    fa[i] = i;
  for (int i = 1; i <= n; i++)
    id[i][0] = ++cnt;
  memset(dist, -1, sizeof(dist));
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    if (!id[u][c])
      id[u][c] = ++cnt;
    u = find(id[u][c]);
    if (!id[v][c])
      id[v][c] = ++cnt;
    v = find(id[v][c]);
    fa[v] = u;
  }
  for (int i = 1; i <= n; i++)
    for (map<int, int>::iterator it = id[i].begin(); it != id[i].end(); it++) {
      int f1 = find(it->second);
      G[i].push_back(f1);
      G[f1].push_back(i);
    }
  bfs();
  printf("%d\n", dist[n] == -1 ? (-1) : (dist[n] / 2));
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define maxn 1000005
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int> PII;
int n, m, cnt, dist[maxn], fa[maxn];
vector<int> G[maxn];
queue<int> q;
map<int, int> id[maxn];
int find(int x) {
  if (fa[x] == x)
    return x;
  return fa[x] = find(fa[x]);
}
void bfs() {
  dist[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = find(G[u][i]);
      if (dist[v] < 0) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i < maxn; i++)
    fa[i] = i;
  for (int i = 1; i <= n; i++)
    id[i][0] = ++cnt;
  memset(dist, -1, sizeof(dist));
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    if (!id[u][c])
      id[u][c] = ++cnt;
    u = find(id[u][c]);
    if (!id[v][c])
      id[v][c] = ++cnt;
    v = find(id[v][c]);
    fa[v] = u;
  }
  for (int i = 1; i <= n; i++)
    for (map<int, int>::iterator it = id[i].begin(); it != id[i].end(); it++) {
      int f1 = find(it->second);
      G[i].push_back(f1);
      G[f1].push_back(i);
    }
  bfs();
  printf("%d\n", dist[n] == -1 ? (-1) : (dist[n] / 2));
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define reg register
typedef long long ll;
using namespace std;
const int MN = 1e5 + 5;
const int MM = 4e5 + 5;
int n, m, tot;
int to[MM * 2], nxt[MM * 2], c[MM * 2], h[MM + MN], cnt;
inline void ins(int s, int t, int c) {
  to[++cnt] = t;
  nxt[cnt] = h[s];
  ::c[cnt] = c;
  h[s] = cnt;
  to[++cnt] = s;
  nxt[cnt] = h[t];
  ::c[cnt] = c;
  h[t] = cnt;
}
int dis[MN + MM];
map<int, int> mp[MN];
typedef pair<int, int> pii;
priority_queue<pii, vector<pii>, greater<pii>> q;
int main() {
  scanf("%d%d", &n, &m);
  tot = n;
  for (reg int i = 1, s, t, c; i <= m; i++) {
    scanf("%d%d%d", &s, &t, &c);
    if (!mp[s][c])
      mp[s][c] = ++tot, ins(mp[s][c], s, 1);
    if (!mp[t][c])
      mp[t][c] = ++tot, ins(mp[t][c], t, 1);
    ins(mp[s][c], mp[t][c], 0);
  }
  memset(dis, 0x3f, sizeof(dis));
  dis[1] = 0;
  q.push(make_pair(0, 1));
  while (!q.empty()) {
    reg pii v = q.top();
    q.pop();
    if (v.first != dis[v.second])
      continue;
    for (reg int i = h[v.second]; i; i = nxt[i])
      if (dis[to[i]] > v.first + c[i])
        dis[to[i]] = v.first + c[i], q.push(make_pair(dis[to[i]], to[i]));
  }
  if (dis[n] == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d\n", dis[n] / 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define reg register
typedef long long ll;
using namespace std;
const int MN = 1e5 + 5;
const int MM = 4e5 + 5;
int n, m, tot;
int to[MM * 5], nxt[MM * 5], c[MM * 5], h[MM + MN], cnt;
inline void ins(int s, int t, int c) {
  to[++cnt] = t;
  nxt[cnt] = h[s];
  ::c[cnt] = c;
  h[s] = cnt;
  to[++cnt] = s;
  nxt[cnt] = h[t];
  ::c[cnt] = c;
  h[t] = cnt;
}
int dis[MN + MM];
map<int, int> mp[MN];
typedef pair<int, int> pii;
priority_queue<pii, vector<pii>, greater<pii>> q;
int main() {
  scanf("%d%d", &n, &m);
  tot = n;
  for (reg int i = 1, s, t, c; i <= m; i++) {
    scanf("%d%d%d", &s, &t, &c);
    if (!mp[s][c])
      mp[s][c] = ++tot, ins(mp[s][c], s, 1);
    if (!mp[t][c])
      mp[t][c] = ++tot, ins(mp[t][c], t, 1);
    ins(mp[s][c], mp[t][c], 0);
  }
  memset(dis, 0x3f, sizeof(dis));
  dis[1] = 0;
  q.push(make_pair(0, 1));
  while (!q.empty()) {
    reg pii v = q.top();
    q.pop();
    if (v.first != dis[v.second])
      continue;
    for (reg int i = h[v.second]; i; i = nxt[i])
      if (dis[to[i]] > v.first + c[i])
        dis[to[i]] = v.first + c[i], q.push(make_pair(dis[to[i]], to[i]));
  }
  if (dis[n] == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d\n", dis[n] / 2);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb emplace_back
#define ll long long
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int N = int(4e5) + 7;
const int mod = int(1e9) + 7;

typedef pair<int, int> pii;
int n, m, w, u, v, c, id;
map<pii, int> mmap;
vector<pii> a[N];
deque<int> q;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
#define Task "test"
  if (fopen(Task ".inp", "r")) {
    freopen(Task ".inp", "r", stdin);
    freopen(Task ".out", "w", stdout);
  }
  cin >> n >> m;
  id = n;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v >> c;
    int &idu = mmap[mp(u, c)];
    int &idv = mmap[mp(v, c)];
    if (idu == 0)
      idu = ++id;
    if (idv == 0)
      idv = ++id;
    a[idu].pb(idv, 0), a[idv].pb(idu, 0);
    a[u].pb(idu, 1), a[idu].pb(u, 0);
    a[v].pb(idv, 1), a[idv].pb(v, 0);
  }
  vector<int> dis(id + 1, mod);
  dis[1] = 0;
  q.push_back(1);
  while (q.size()) {
    u = q.front();
    q.pop_front();
    for (pii p : a[u]) {
      w = p.se, v = p.fi;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        if (!w)
          q.push_front(v);
        else
          q.push_back(v);
      }
    }
  }
  if (dis[n] == mod)
    cout << -1;
  else
    cout << dis[n];
}


Fixed submission:
#include <bits/stdc++.h>
#define pb emplace_back
#define ll long long
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int N = int(5e5) + 7;
const int mod = int(1e9) + 7;

typedef pair<int, int> pii;
int n, m, w, u, v, c, id;
map<pii, int> mmap;
vector<pii> a[N];
deque<int> q;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
#define Task "test"
  if (fopen(Task ".inp", "r")) {
    freopen(Task ".inp", "r", stdin);
    freopen(Task ".out", "w", stdout);
  }
  cin >> n >> m;
  id = n;
  for (int i = 0; i < m; ++i) {
    cin >> u >> v >> c;
    int &idu = mmap[mp(u, c)];
    int &idv = mmap[mp(v, c)];
    if (idu == 0)
      idu = ++id;
    if (idv == 0)
      idv = ++id;
    a[idu].pb(idv, 0), a[idv].pb(idu, 0);
    a[u].pb(idu, 1), a[idu].pb(u, 0);
    a[v].pb(idv, 1), a[idv].pb(v, 0);
  }
  vector<int> dis(id + 1, mod);
  dis[1] = 0;
  q.push_back(1);
  while (q.size()) {
    u = q.front();
    q.pop_front();
    for (pii p : a[u]) {
      w = p.se, v = p.fi;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        if (!w)
          q.push_front(v);
        else
          q.push_back(v);
      }
    }
  }
  if (dis[n] == mod)
    cout << -1;
  else
    cout << dis[n];
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define fi first
#define se second
#define ii pair<int, int>
#define pb push_back
#define mp make_pair

const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const int oo = 1e9;

int n, m, cnt;
int d[N];
vector<ii> vi[N];
map<ii, int> ma;

void addeg(int u, int v, int ci) {
  vi[u].pb(ii(v, ci));
  vi[v].pb(ii(u, ci));
}

void Solve() {
  FOR(i, 1, cnt) d[i] = oo;
  priority_queue<ii, vector<ii>, greater<ii>> pq;

  d[1] = 0;
  pq.push(ii(0, 1));

  while (!pq.empty()) {
    int u = pq.top().se, du = pq.top().fi;
    pq.pop();
    if (d[u] != du)
      continue;

    for (ii v : vi[u])
      if (d[v.fi] > du + v.se) {
        d[v.fi] = du + v.se;
        pq.push(ii(du + v.se, v.fi));
      }
  }
  if (d[n] == oo)
    cout << "-1";
  else
    cout << d[n] / 2;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  cnt = n;

  FOR(i, 1, m) {
    int u, v, ci;
    cin >> u >> v >> ci;
    int nex = ma[ii(u, ci)];
    if (!nex) {
      ma[ii(u, ci)] = nex = ++cnt;
    }

    int rev = ma[ii(v, ci)];
    if (!rev) {
      ma[ii(v, ci)] = rev = ++cnt;
    }
    addeg(nex, rev, 0);
    addeg(nex, u, 1);
    addeg(rev, v, 1);
  }
  Solve();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define FOR(a, b, c) for (int a = b; a <= c; ++a)
#define fi first
#define se second
#define ii pair<int, int>
#define pb push_back
#define mp make_pair

const int N = 8e5 + 10;
const int MOD = 1e9 + 7;
const int oo = 1e9;

int n, m, cnt;
int d[N];
vector<ii> vi[N];
map<ii, int> ma;

void addeg(int u, int v, int ci) {
  vi[u].pb(ii(v, ci));
  vi[v].pb(ii(u, ci));
}

void Solve() {
  FOR(i, 1, cnt) d[i] = oo;
  priority_queue<ii, vector<ii>, greater<ii>> pq;

  d[1] = 0;
  pq.push(ii(0, 1));

  while (!pq.empty()) {
    int u = pq.top().se, du = pq.top().fi;
    pq.pop();
    if (d[u] != du)
      continue;

    for (ii v : vi[u])
      if (d[v.fi] > du + v.se) {
        d[v.fi] = du + v.se;
        pq.push(ii(du + v.se, v.fi));
      }
  }
  if (d[n] == oo)
    cout << "-1";
  else
    cout << d[n] / 2;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  cnt = n;

  FOR(i, 1, m) {
    int u, v, ci;
    cin >> u >> v >> ci;
    int nex = ma[ii(u, ci)];
    if (!nex) {
      ma[ii(u, ci)] = nex = ++cnt;
    }

    int rev = ma[ii(v, ci)];
    if (!rev) {
      ma[ii(v, ci)] = rev = ++cnt;
    }
    addeg(nex, rev, 0);
    addeg(nex, u, 1);
    addeg(rev, v, 1);
  }
  Solve();
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 500010;
const int inf = 0x3f3f3f3f;

int n, m, ids, tot;
map<pair<int, int>, int> H;

inline int getid(int u, int v) {
  if (H.find(make_pair(u, v)) != H.end())
    return H[make_pair(u, v)];
  return H[make_pair(u, v)] = ++ids;
}

struct Edge {
  int to, nxt;
  int w;
} e[MAXN << 1];
int h[MAXN];

inline void add(int u, int v, int w) {
  e[++tot] = (Edge){v, h[u], w};
  h[u] = tot;
  e[++tot] = (Edge){u, h[v], w};
  h[v] = tot;
}

struct Node {
  int u;
  int d;
  bool operator<(const Node &x) const { return d > x.d; }
};
priority_queue<Node> q;
int dis[MAXN];

void dijkstra() {
  for (int i = 1; i <= ids; ++i)
    dis[i] = inf;
  dis[1] = 0;
  q.push((Node){1, 0});
  while (!q.empty()) {
    Node x = q.top();
    q.pop();
    if (dis[x.u] != x.d)
      continue;
    for (int i = h[x.u]; i; i = e[i].nxt) {
      int y = e[i].to;
      int z = e[i].w;
      if (dis[y] > dis[x.u] + z) {
        dis[y] = dis[x.u] + z;
        q.push((Node){y, dis[y]});
      }
    }
  }
}

int main() {
  scanf("%d %d", &n, &m);
  ids = n;
  for (int i = 1; i <= m; ++i) {
    int u, v, z;
    scanf("%d %d %d", &u, &v, &z);
    int a = getid(u, z), b = getid(v, z);
    add(u, a, 1), add(a, b, 0), add(v, b, 1);
  }
  dijkstra();
  printf("%d\n", dis[n] == inf ? -1 : dis[n] >> 1);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000010;
const int inf = 0x3f3f3f3f;

int n, m, ids, tot;
map<pair<int, int>, int> H;

inline int getid(int u, int v) {
  if (H.find(make_pair(u, v)) != H.end())
    return H[make_pair(u, v)];
  return H[make_pair(u, v)] = ++ids;
}

struct Edge {
  int to, nxt;
  int w;
} e[MAXN << 1];
int h[MAXN];

inline void add(int u, int v, int w) {
  e[++tot] = (Edge){v, h[u], w};
  h[u] = tot;
  e[++tot] = (Edge){u, h[v], w};
  h[v] = tot;
}

struct Node {
  int u;
  int d;
  bool operator<(const Node &x) const { return d > x.d; }
};
priority_queue<Node> q;
int dis[MAXN];

void dijkstra() {
  for (int i = 1; i <= ids; ++i)
    dis[i] = inf;
  dis[1] = 0;
  q.push((Node){1, 0});
  while (!q.empty()) {
    Node x = q.top();
    q.pop();
    if (dis[x.u] != x.d)
      continue;
    for (int i = h[x.u]; i; i = e[i].nxt) {
      int y = e[i].to;
      int z = e[i].w;
      if (dis[y] > dis[x.u] + z) {
        dis[y] = dis[x.u] + z;
        q.push((Node){y, dis[y]});
      }
    }
  }
}

int main() {
  scanf("%d %d", &n, &m);
  ids = n;
  for (int i = 1; i <= m; ++i) {
    int u, v, z;
    scanf("%d %d %d", &u, &v, &z);
    int a = getid(u, z), b = getid(v, z);
    add(u, a, 1), add(a, b, 0), add(v, b, 1);
  }
  dijkstra();
  printf("%d\n", dis[n] == inf ? -1 : dis[n] >> 1);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
/// In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MXN = 2e5 + 10;
int n, m, Root;
int dis[MXN];
vector<pair<int, int>> adj[MXN];
queue<int> q;
map<pair<int, int>, bool> mp;
void dfs(ll u, ll c) {
  for (auto Pr : adj[u]) {
    int v = Pr.first, cp = Pr.second;
    if (cp != c || dis[v] < dis[Root] || mp[{v, c}])
      continue;
    mp[{v, c}] = 1;
    if (dis[v] > dis[Root] + 1)
      q.push(v);
    dis[v] = min(dis[v], dis[Root] + 1);
    dfs(v, c);
  }
}
void BFS() {
  dis[1] = 0, q.push(1);
  while (!q.empty()) {
    int u = q.front();
    Root = u;
    q.pop();
    for (auto Pr : adj[u]) {
      ll v = Pr.first, c = Pr.second;
      if (dis[v] < dis[u] || mp[{v, c}])
        continue;
      mp[{v, c}] = mp[{u, c}] = 1;
      if (dis[v] > dis[u] + 1)
        q.push(v);
      dis[v] = min(dis[v], dis[u] + 1), dfs(v, c);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m, memset(dis, 31, sizeof dis);
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    adj[u].push_back({v, c}), adj[v].push_back({u, c});
  }
  BFS();
  cout << (dis[n] > 1e8 ? -1 : dis[n]);
  return 0;
}
/*!
    I CHOOSE TO HAVE FAITH,
    BECAUSE WITHOUT THAT,
    I HAVE NOTHING....
    ITS THE ONLY THING THAT'S KEEPING ME GOING.
*/
//! N.N


Fixed submission:
/// In the name of GOD
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MXN = 2e5 + 10;
int n, m, Root;
int dis[MXN];
vector<pair<int, int>> adj[MXN];
queue<int> q;
map<pair<int, int>, bool> mp;
void dfs(ll u, ll c) {
  if (u == n) {
    cout << dis[n];
    exit(0);
  }
  for (auto Pr : adj[u]) {
    int v = Pr.first, cp = Pr.second;
    if (cp != c || dis[v] < dis[Root] || mp[{v, c}])
      continue;
    mp[{v, c}] = 1;
    if (dis[v] > dis[Root] + 1)
      q.push(v);
    dis[v] = min(dis[v], dis[Root] + 1);
    dfs(v, c);
  }
}
void BFS() {
  dis[1] = 0, q.push(1);
  while (!q.empty()) {
    int u = q.front();
    Root = u;
    q.pop();
    for (auto Pr : adj[u]) {
      ll v = Pr.first, c = Pr.second;
      if (dis[v] < dis[u] || mp[{v, c}])
        continue;
      mp[{v, c}] = mp[{u, c}] = 1;
      if (dis[v] > dis[u] + 1)
        q.push(v);
      dis[v] = min(dis[v], dis[u] + 1), dfs(v, c);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m, memset(dis, 31, sizeof dis);
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    adj[u].push_back({v, c}), adj[v].push_back({u, c});
  }
  BFS();
  cout << (dis[n] > 1e8 ? -1 : dis[n]);
  return 0;
}
/*!
    I CHOOSE TO HAVE FAITH,
    BECAUSE WITHOUT THAT,
    I HAVE NOTHING....
    ITS THE ONLY THING THAT'S KEEPING ME GOING.
*/
//! N.N
Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 0x7fffffff
#define ll long long
#define P pair<int, int>
#define MP(a, b) make_pair(a, b)
using namespace std;
int n, m, dis[200005], head, tail, t, h[200005];
priority_queue<P> q;
set<int> in[200005];
struct E {
  int v, c, next;
} g[400005];
void ins(int u, int v, int c) {
  g[++t].v = v, g[t].c = c, g[t].next = h[u], h[u] = t;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int main() {
  memset(dis, 0x3f, sizeof dis);
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    int u, v, c;
    u = read(), v = read(), c = read();
    ins(u, v, c);
    ins(v, u, c);
  }
  q.push(MP(0, 1));
  dis[1] = 0;
  while (!q.empty()) {
    int u = q.top().second, cost = -q.top().first;
    if (cost > dis[u])
      continue;
    for (int j = h[u]; j; j = g[j].next) {
      int v = g[j].v, c = g[j].c, d = dis[u] + (!in[u].count(c));
      if (d > dis[v])
        continue;
      if (d < dis[v]) {
        q.push(MP(-d, v));
        dis[v] = d;
        in[v].clear();
      }
      in[v].insert(c);
    }
  }
  printf("%d", dis[n] == 1061109567 ? -1 : dis[n]);
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 0x7fffffff
#define ll long long
#define P pair<int, int>
#define MP(a, b) make_pair(a, b)
using namespace std;
int n, m, dis[200005], head, tail, t, h[200005];
priority_queue<P> q;
set<int> in[200005];
struct E {
  int v, c, next;
} g[400005];
void ins(int u, int v, int c) {
  g[++t].v = v, g[t].c = c, g[t].next = h[u], h[u] = t;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int main() {
  memset(dis, 0x3f, sizeof dis);
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    int u, v, c;
    u = read(), v = read(), c = read();
    ins(u, v, c);
    ins(v, u, c);
  }
  q.push(MP(0, 1));
  dis[1] = 0;
  while (!q.empty()) {
    int u = q.top().second, cost = -q.top().first;
    q.pop();
    if (cost > dis[u])
      continue;
    for (int j = h[u]; j; j = g[j].next) {
      int v = g[j].v, c = g[j].c, d = dis[u] + (!in[u].count(c));
      if (d > dis[v])
        continue;
      if (d < dis[v]) {
        q.push(MP(-d, v));
        dis[v] = d;
        in[v].clear();
      }
      in[v].insert(c);
    }
  }
  printf("%d", dis[n] == 1061109567 ? -1 : dis[n]);
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

struct dsu_t {
  vector<int> dsu;
  dsu_t(int n) : dsu(n) { iota(dsu.begin(), dsu.end(), 0); }
  int &operator[](int i) {
    int p = i, f;
    while (i != dsu[i])
      i = dsu[i];
    while (p != dsu[p])
      f = dsu[p], dsu[p] = i, p = f;
    return dsu[i];
  }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  dsu_t dsu(n + m + m);
  map<long long, int> index;
  auto get = [&](int a) {
    auto it = index.find(a);
    if (it == index.end())
      it = index.emplace(a, index.size()).first;
    return it->second;
  };
  for (int i = 0, u, v, c; i < m; i++) {
    scanf("%d%d%d", &u, &v, &c);
    --u, --v, --c;
    u = dsu[get(1ll * c * n + u)];
    v = dsu[get(1ll * c * n + v)];
    if (u != v)
      dsu[u] = v;
  }
  vector<vector<int>> G(n + m + m);
  for (pair<const long long, int> it : index) {
    it.second = dsu[it.second] + n;
    G[it.first % n].push_back(it.second);
    G[it.second].push_back(it.first % n);
  }
  vector<int> dis(n + m + m, INF);
  queue<pair<int, int>> q;
  q.emplace(0, 0);
  dis[0] = 0;
  for (int u, v, w; !q.empty();) {
    tie(w, u) = q.front();
    q.pop();
    for (const int v : G[u]) {
      if (dis[u] + 1 < dis[v]) {
        dis[v] = dis[u] + 1;
        q.emplace(dis[v], v);
      }
    }
  }
  printf(dis[n - 1] == INF ? "-1\n" : "%d\n", dis[n - 1] / 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

struct dsu_t {
  vector<int> dsu;
  dsu_t(int n) : dsu(n) { iota(dsu.begin(), dsu.end(), 0); }
  int &operator[](int i) {
    int p = i, f;
    while (i != dsu[i])
      i = dsu[i];
    while (p != dsu[p])
      f = dsu[p], dsu[p] = i, p = f;
    return dsu[i];
  }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  dsu_t dsu(n + m + m);
  map<long long, int> index;
  auto get = [&](long long a) {
    auto it = index.find(a);
    if (it == index.end())
      it = index.emplace(a, index.size()).first;
    return it->second;
  };
  for (int i = 0, u, v, c; i < m; i++) {
    scanf("%d%d%d", &u, &v, &c);
    --u, --v, --c;
    u = dsu[get(1ll * c * n + u)];
    v = dsu[get(1ll * c * n + v)];
    if (u != v)
      dsu[u] = v;
  }
  vector<vector<int>> G(n + m + m);
  for (pair<const long long, int> it : index) {
    it.second = dsu[it.second] + n;
    G[it.first % n].push_back(it.second);
    G[it.second].push_back(it.first % n);
  }
  vector<int> dis(n + m + m, INF);
  queue<pair<int, int>> q;
  q.emplace(0, 0);
  dis[0] = 0;
  for (int u, v, w; !q.empty();) {
    tie(w, u) = q.front();
    q.pop();
    for (const int v : G[u]) {
      if (dis[u] + 1 < dis[v]) {
        dis[v] = dis[u] + 1;
        q.emplace(dis[v], v);
      }
    }
  }
  printf(dis[n - 1] == INF ? "-1\n" : "%d\n", dis[n - 1] / 2);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 112345;
const int inf = 0x3f3f3f3f;
int n, m, p[maxn], q[maxn], c[maxn], d[maxn];
vector<long long> id;
vector<pii> G[maxn];
priority_queue<pii, vector<pii>, greater<pii>> que;
inline int get(long long x) {
  return lower_bound(id.begin(), id.end(), x) - id.begin() + 1;
}
inline void addedge(int u, int v, int w) {
  G[u].push_back(pii(v, w));
  G[v].push_back(pii(u, w));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i)
    scanf("%d%d%d", &p[i], &q[i], &c[i]);
  for (int i = 1; i <= n; ++i)
    id.push_back(i);
  for (int i = 1; i <= m; ++i) {
    id.push_back(p[i] * 1000000ll + c[i]);
    id.push_back(q[i] * 1000000ll + c[i]);
  }
  sort(id.begin(), id.end());
  for (int i = 1; i <= m; ++i) {
    addedge(get(p[i]), get(p[i] * 1000000ll + c[i]), 1);
    addedge(get(q[i]), get(q[i] * 1000000ll + c[i]), 1);
    addedge(get(p[i] * 1000000ll + c[i]), get(q[i] * 1000000ll + c[i]), 0);
  }
  memset(d, 0x3f, sizeof d);
  d[get(1)] = 0;
  que.push(pii(0, get(1)));
  while (!que.empty()) {
    int u = que.top().second;
    int o = que.top().first;
    que.pop();
    if (o > d[u])
      continue;
    for (int i = 0; i < (int)G[u].size(); ++i) {
      int v = G[u][i].first;
      int w = G[u][i].second;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        que.push(pii(d[v], v));
      }
    }
  }
  if (d[n] == inf)
    d[n] = -2;
  printf("%d\n", d[n] / 2);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 512345;
const int inf = 0x3f3f3f3f;
int n, m, p[maxn], q[maxn], c[maxn], d[maxn];
vector<long long> id;
vector<pii> G[maxn];
priority_queue<pii, vector<pii>, greater<pii>> que;
inline int get(long long x) {
  return lower_bound(id.begin(), id.end(), x) - id.begin() + 1;
}
inline void addedge(int u, int v, int w) {
  G[u].push_back(pii(v, w));
  G[v].push_back(pii(u, w));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i)
    scanf("%d%d%d", &p[i], &q[i], &c[i]);
  for (int i = 1; i <= n; ++i)
    id.push_back(i);
  for (int i = 1; i <= m; ++i) {
    id.push_back(p[i] * 1000000ll + c[i]);
    id.push_back(q[i] * 1000000ll + c[i]);
  }
  sort(id.begin(), id.end());
  for (int i = 1; i <= m; ++i) {
    addedge(get(p[i]), get(p[i] * 1000000ll + c[i]), 1);
    addedge(get(q[i]), get(q[i] * 1000000ll + c[i]), 1);
    addedge(get(p[i] * 1000000ll + c[i]), get(q[i] * 1000000ll + c[i]), 0);
  }
  memset(d, 0x3f, sizeof d);
  d[get(1)] = 0;
  que.push(pii(0, get(1)));
  while (!que.empty()) {
    int u = que.top().second;
    int o = que.top().first;
    que.pop();
    if (o > d[u])
      continue;
    for (int i = 0; i < (int)G[u].size(); ++i) {
      int v = G[u][i].first;
      int w = G[u][i].second;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        que.push(pii(d[v], v));
      }
    }
  }
  if (d[n] == inf)
    d[n] = -2;
  printf("%d\n", d[n] / 2);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 4e5 + 10;
int n, m, d[M], cnt;
map<int, int> g[N];
deque<int> Q;
int head[M], to[M << 2], nxt[M << 2], w[M << 2], tot;

inline int rd() {
  char ch = getchar();
  int x = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}

inline int get(int x, int y) {
  if (g[x].find(y) != g[x].end())
    return g[x][y];
  else
    return (g[x][y] = ++cnt);
}

inline void lk(int u, int v, int cc) {
  to[++tot] = v;
  nxt[tot] = head[u];
  head[u] = tot;
  w[tot] = cc;
}

int main() {
  int i, j, x0, y0, xc, yc, ix, iy, iz;
  memset(d, 0x3f, sizeof(d));
  n = rd();
  m = rd();
  for (i = 1; i <= n; ++i)
    g[i][0] = ++cnt;
  for (; m; m--) {
    ix = rd();
    iy = rd();
    iz = rd();
    x0 = get(ix, 0);
    y0 = get(iy, 0);
    xc = get(ix, iz);
    yc = get(iy, iz);
    lk(xc, yc, 0);
    lk(yc, xc, 0);
    lk(x0, yc, 1);
    lk(yc, x0, 1);
    lk(xc, y0, 1);
    lk(y0, xc, 1);
  }
  d[1] = 0;
  Q.push_back(1);
  for (; !Q.empty();) {
    ix = Q.back();
    Q.pop_back();
    for (i = head[ix]; i; i = nxt[i]) {
      j = to[i];
      if (d[j] > d[ix] + w[i]) {
        d[j] = d[ix] + w[i];
        if (w[i] == 1)
          Q.push_front(j);
        else
          Q.push_back(j);
      }
    }
  }
  if (d[n] > M)
    puts("-1");
  else
    printf("%d\n", d[n] >> 1);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 5e5 + 10;
int n, m, d[M], cnt;
map<int, int> g[N];
deque<int> Q;
int head[M], to[M << 2], nxt[M << 2], w[M << 2], tot;

inline int rd() {
  char ch = getchar();
  int x = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}

inline int get(int x, int y) {
  if (g[x].find(y) != g[x].end())
    return g[x][y];
  else
    return (g[x][y] = ++cnt);
}

inline void lk(int u, int v, int cc) {
  to[++tot] = v;
  nxt[tot] = head[u];
  head[u] = tot;
  w[tot] = cc;
}

int main() {
  int i, j, x0, y0, xc, yc, ix, iy, iz;
  memset(d, 0x3f, sizeof(d));
  n = rd();
  m = rd();
  for (i = 1; i <= n; ++i)
    g[i][0] = ++cnt;
  for (; m; m--) {
    ix = rd();
    iy = rd();
    iz = rd();
    x0 = get(ix, 0);
    y0 = get(iy, 0);
    xc = get(ix, iz);
    yc = get(iy, iz);
    lk(xc, yc, 0);
    lk(yc, xc, 0);
    lk(x0, yc, 1);
    lk(yc, x0, 1);
    lk(xc, y0, 1);
    lk(y0, xc, 1);
  }
  d[1] = 0;
  Q.push_back(1);
  for (; !Q.empty();) {
    ix = Q.back();
    Q.pop_back();
    for (i = head[ix]; i; i = nxt[i]) {
      j = to[i];
      if (d[j] > d[ix] + w[i]) {
        d[j] = d[ix] + w[i];
        if (w[i] == 1)
          Q.push_front(j);
        else
          Q.push_back(j);
      }
    }
  }
  if (d[n] > M)
    puts("-1");
  else
    printf("%d\n", d[n] >> 1);
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int maxn = 500020;
using namespace std;
int n, m, x, y, c, cnt;
map<int, int> g[100020];
int get(int x, int y) {
  if (g[x].count(y)) {
    return g[x][y];
  }
  return g[x][y] = ++cnt;
}
struct edge {
  int to, w, next;
} e[500020];
int sz = 0, head[500020], dis[500020], vis[500020];
void insert(int u, int v, int w) {
  e[++sz].to = v;
  e[sz].w = w;
  e[sz].next = head[u];
  head[u] = sz;
  e[++sz].to = u;
  e[sz].w = w;
  e[sz].next = head[v];
  head[v] = sz;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    get(i, 0);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &c);
    int x0 = get(x, 0), xc = get(x, c);
    int y0 = get(y, 0), yc = get(y, c);
    insert(xc, yc, 0);
    insert(x0, xc, 1);
    insert(y0, yc, 1);
  }
  memset(dis, -1, sizeof(dis));
  typedef pair<int, int> pii;
  priority_queue<pii, vector<pii>, greater<pii>> q;
  dis[1] = 0;
  q.push(make_pair(0, 1));
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].to;
      if (dis[u] + e[i].w < dis[v] || dis[v] == -1) {
        dis[v] = dis[u] + e[i].w;
        q.push(make_pair(dis[v], v));
      }
    }
  }
  printf("%d\n", dis[n] >> 1);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
const int maxn = 500020;
using namespace std;
int n, m, x, y, c, cnt;
map<int, int> g[100020];
int get(int x, int y) {
  if (g[x].count(y)) {
    return g[x][y];
  }
  return g[x][y] = ++cnt;
}
struct edge {
  int to, w, next;
} e[maxn * 20];
int sz = 0, head[maxn * 10], dis[maxn * 10], vis[maxn * 10];
void insert(int u, int v, int w) {
  e[++sz].to = v;
  e[sz].w = w;
  e[sz].next = head[u];
  head[u] = sz;
  e[++sz].to = u;
  e[sz].w = w;
  e[sz].next = head[v];
  head[v] = sz;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    get(i, 0);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &c);
    int x0 = get(x, 0), xc = get(x, c);
    int y0 = get(y, 0), yc = get(y, c);
    insert(xc, yc, 0);
    insert(x0, xc, 1);
    insert(y0, yc, 1);
  }
  memset(dis, -1, sizeof(dis));
  typedef pair<int, int> pii;
  priority_queue<pii, vector<pii>, greater<pii>> q;
  dis[1] = 0;
  q.push(make_pair(0, 1));
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].to;
      if (dis[u] + e[i].w < dis[v] || dis[v] == -1) {
        dis[v] = dis[u] + e[i].w;
        q.push(make_pair(dis[v], v));
      }
    }
  }
  printf("%d\n", dis[n] >> 1);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MN 300000
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, m, head[MN + 5], cnt, num, vis[MN + 5], id[MN + 5], q[MN + 5], top,
    d[MN + 5];
struct edge {
  int to, next, w;
} e[MN * 4 + 5];
vector<pair<int, int>> v[1000005];
inline void ins(int f, int t, int w) {
  e[++cnt] = (edge){t, head[f], w};
  head[f] = cnt;
}
void Update(int x) {
  for (int i = head[x]; i; i = e[i].next)
    if (d[x] + e[i].w < d[e[i].to]) {
      d[e[i].to] = d[x] + e[i].w;
      if (e[i].w)
        q[++top] = e[i].to;
      else
        Update(e[i].to);
    }
}
int main() {
  n = num = read();
  m = read();
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read(), c = read();
    v[c].push_back(make_pair(x, y));
  }
  for (int i = 1; i <= 1000000; ++i)
    for (int j = 0; j < v[i].size(); ++j) {
      int x = v[i][j].first, y = v[i][j].second;
      if (vis[x] != i)
        vis[x] = i, id[x] = ++num, ins(num, x, 0), ins(x, num, 1);
      if (vis[y] != i)
        vis[y] = i, id[y] = ++num, ins(num, y, 0), ins(y, num, 1);
      ins(id[x], id[y], 0);
      ins(id[y], id[x], 0);
    }
  memset(d, 40, sizeof(d));
  d[q[top = 1] = 1] = 0;
  for (int i = 1; i <= top; ++i)
    Update(q[i]);
  printf("%d\n", d[n] > 1e8 ? -1 : d[n]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MN 500000
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, m, head[MN + 5], cnt, num, vis[MN + 5], id[MN + 5], q[MN + 5], top,
    d[MN + 5];
struct edge {
  int to, next, w;
} e[MN * 4 + 5];
vector<pair<int, int>> v[1000005];
inline void ins(int f, int t, int w) {
  e[++cnt] = (edge){t, head[f], w};
  head[f] = cnt;
}
void Update(int x) {
  for (int i = head[x]; i; i = e[i].next)
    if (d[x] + e[i].w < d[e[i].to]) {
      d[e[i].to] = d[x] + e[i].w;
      if (e[i].w)
        q[++top] = e[i].to;
      else
        Update(e[i].to);
    }
}
int main() {
  n = num = read();
  m = read();
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read(), c = read();
    v[c].push_back(make_pair(x, y));
  }
  for (int i = 1; i <= 1000000; ++i)
    for (int j = 0; j < v[i].size(); ++j) {
      int x = v[i][j].first, y = v[i][j].second;
      if (vis[x] != i)
        vis[x] = i, id[x] = ++num, ins(num, x, 0), ins(x, num, 1);
      if (vis[y] != i)
        vis[y] = i, id[y] = ++num, ins(num, y, 0), ins(y, num, 1);
      ins(id[x], id[y], 0);
      ins(id[y], id[x], 0);
    }
  memset(d, 40, sizeof(d));
  d[q[top = 1] = 1] = 0;
  for (int i = 1; i <= top; ++i)
    Update(q[i]);
  printf("%d\n", d[n] > 1e8 ? -1 : d[n]);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define N 100100
#define M 200100
using namespace std;
int a[M], b[M], c[M];
vector<int> g[M + N];
int dis[M + N];
int f[M];
int pre[M];
int find(int x) {
  if (f[x] == x)
    return x;
  return f[x] = find(f[x]);
}
void uni(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy)
    f[fx] = fy;
}
int main() {
  int n, m;
  memset(dis, -1, sizeof(dis));
  memset(pre, -1, sizeof(pre));
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    g[a[i] + m].push_back(i);
    g[b[i] + m].push_back(i);
    f[i] = i;
  }
  for (int i = m + 1; i <= m + n; i++) {
    for (int j = 0; j < g[i].size(); j++) {
      int x = g[i][j];
      if (pre[c[x]] >= 0)
        uni(pre[c[x]], x);
      pre[c[x]] = x;
    }
    for (int j = 0; j < g[i].size(); j++) {
      int x = g[i][j];
      pre[c[x]] = -1;
    }
  }
  for (int i = 0; i < m; i++) {
    g[find(i)].push_back(a[i] + m);
    g[find(i)].push_back(b[i] + m);
  }
  for (int i = m + 1; i <= m + n; i++) {
    for (int j = 0; j < g[i].size(); j++) {
      g[i][j] = find(g[i][j]);
    }
  }
  queue<int> q;
  q.push(m + 1);
  dis[m + 1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < g[u].size(); i++) {
      if (dis[g[u][i]] < 0) {
        dis[g[u][i]] = dis[u] + 1;
        q.push(g[u][i]);
      }
    }
  }
  if (dis[m + n] < 0)
    printf("%d\n", -1);
  else
    printf("%d\n", dis[m + n] / 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 100100
#define M 200100
using namespace std;
int a[M], b[M], c[M];
vector<int> g[M + N];
int dis[M + N];
int f[M];
int pre[1001000];
int find(int x) {
  if (f[x] == x)
    return x;
  return f[x] = find(f[x]);
}
void uni(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy)
    f[fx] = fy;
}
int main() {
  int n, m;
  memset(dis, -1, sizeof(dis));
  memset(pre, -1, sizeof(pre));
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    g[a[i] + m].push_back(i);
    g[b[i] + m].push_back(i);
    f[i] = i;
  }
  for (int i = m + 1; i <= m + n; i++) {
    for (int j = 0; j < g[i].size(); j++) {
      int x = g[i][j];
      if (pre[c[x]] >= 0)
        uni(pre[c[x]], x);
      pre[c[x]] = x;
    }
    for (int j = 0; j < g[i].size(); j++) {
      int x = g[i][j];
      pre[c[x]] = -1;
    }
  }
  for (int i = 0; i < m; i++) {
    g[find(i)].push_back(a[i] + m);
    g[find(i)].push_back(b[i] + m);
  }
  for (int i = m + 1; i <= m + n; i++) {
    for (int j = 0; j < g[i].size(); j++) {
      g[i][j] = find(g[i][j]);
    }
  }
  queue<int> q;
  q.push(m + 1);
  dis[m + 1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < g[u].size(); i++) {
      if (dis[g[u][i]] < 0) {
        dis[g[u][i]] = dis[u] + 1;
        q.push(g[u][i]);
      }
    }
  }
  if (dis[m + n] < 0)
    printf("%d\n", -1);
  else
    printf("%d\n", dis[m + n] / 2);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
map<int, int> mp[N];
int num, cnt, head[4 * N], d[4 * N], vis[4 * N];
struct node {
  int nxt, ed, len;
} e[12 * N];
void add(int x, int y, int z) {
  e[cnt].nxt = head[x];
  e[cnt].ed = y;
  e[cnt].len = z;
  head[x] = cnt++;
}
int get_id(int x, int y) {
  if (mp[x].count(y))
    return mp[x][y];
  else
    return mp[x][y] = ++num;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    Q;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  num = n;
  memset(head, -1, sizeof(head));
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    int nx = get_id(x, z), ny = get_id(y, z);
    add(nx, ny, 0), add(ny, nx, 0);
    add(x, nx, 1), add(nx, x, 1);
    add(y, ny, 1), add(ny, y, 1);
  }
  for (int i = 1; i <= num; i++) {
    d[i] = inf;
  }
  d[1] = 0;
  Q.push(make_pair(0, 1));
  while (!Q.empty()) {
    int t = Q.top().second;
    Q.pop();
    if (vis[t])
      continue;
    vis[t] = 1;
    for (int i = head[t]; ~i; i = e[i].nxt) {
      int v = e[i].ed, len = e[i].len;
      if (d[v] > d[t] + len) {
        d[v] = d[t] + len;
        Q.push(make_pair(d[v], v));
      }
    }
  }
  printf("%d\n", d[n] == inf ? -1 : d[n] / 2);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
map<int, int> mp[N];
int num, cnt, head[12 * N], d[12 * N], vis[12 * N];
struct node {
  int nxt, ed, len;
} e[12 * N];
void add(int x, int y, int z) {
  e[cnt].nxt = head[x];
  e[cnt].ed = y;
  e[cnt].len = z;
  head[x] = cnt++;
}
int get_id(int x, int y) {
  if (mp[x].count(y))
    return mp[x][y];
  else
    return mp[x][y] = ++num;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    Q;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  num = n;
  memset(head, -1, sizeof(head));
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    int nx = get_id(x, z), ny = get_id(y, z);
    add(nx, ny, 0), add(ny, nx, 0);
    add(x, nx, 1), add(nx, x, 1);
    add(y, ny, 1), add(ny, y, 1);
  }
  for (int i = 1; i <= num; i++) {
    d[i] = inf;
  }
  d[1] = 0;
  Q.push(make_pair(0, 1));
  while (!Q.empty()) {
    int t = Q.top().second;
    Q.pop();
    if (vis[t])
      continue;
    vis[t] = 1;
    for (int i = head[t]; ~i; i = e[i].nxt) {
      int v = e[i].ed, len = e[i].len;
      if (d[v] > d[t] + len) {
        d[v] = d[t] + len;
        Q.push(make_pair(d[v], v));
      }
    }
  }
  printf("%d\n", d[n] == inf ? -1 : d[n] / 2);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define N 500010
#define inf 0x3f3f3f3f

int head[N], cnt;
int d[N], vis[N], q[N];
int n, m;
struct node {
  int to, nxt, v;
} e[N];

void ins(int u, int v, int w) {
  e[++cnt].to = v;
  e[cnt].nxt = head[u];
  e[cnt].v = w;
  head[u] = cnt;
}

map<pair<int, int>, int> mp;

int tot = 0;

int get_num(int x, int y) {
  if (!mp.count(make_pair(x, y)))
    mp[make_pair(x, y)] = ++tot;
  return mp[make_pair(x, y)];
}

void spfa() {
  for (int i = 1; i <= tot; i++)
    d[i] = inf;
  vis[1] = q[1] = 1;
  d[1] = 0;
  int l = 1, r = 2;
  while (l < r) {
    int u = q[l++];
    vis[u] = 0;
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (d[v] > d[u] + e[i].v) {
        d[v] = d[u] + e[i].v;
        if (!vis[v])
          vis[v] = 1, q[r++] = v;
      }
    }
  }
  if (d[n] == inf)
    puts("-1");
  else
    printf("%d\n", d[n] / 2);
}

int main() {
  scanf("%d%d", &n, &m);
  tot = n;
  for (int i = 1; i <= m; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    int n1 = get_num(x, c), n2 = get_num(y, c);
    ins(x, n1, 1);
    ins(n1, x, 1);
    ins(n1, y, 1);
    ins(y, n1, 1);
    ins(n1, n2, 0);
    ins(n2, n1, 0);
  }
  spfa();
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define N 5000010
#define inf 0x3f3f3f3f

int head[N], cnt;
int d[N], vis[N], q[N];
int n, m;
struct node {
  int to, nxt, v;
} e[N];

void ins(int u, int v, int w) {
  e[++cnt].to = v;
  e[cnt].nxt = head[u];
  e[cnt].v = w;
  head[u] = cnt;
}

map<pair<int, int>, int> mp;

int tot = 0;

int get_num(int x, int y) {
  if (!mp.count(make_pair(x, y)))
    mp[make_pair(x, y)] = ++tot;
  return mp[make_pair(x, y)];
}

void spfa() {
  for (int i = 1; i <= tot; i++)
    d[i] = inf;
  vis[1] = q[1] = 1;
  d[1] = 0;
  int l = 1, r = 2;
  while (l < r) {
    int u = q[l++];
    vis[u] = 0;
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (d[v] > d[u] + e[i].v) {
        d[v] = d[u] + e[i].v;
        if (!vis[v])
          vis[v] = 1, q[r++] = v;
      }
    }
  }
  if (d[n] == inf)
    puts("-1");
  else
    printf("%d\n", d[n] / 2);
}

int main() {
  scanf("%d%d", &n, &m);
  tot = n;
  for (int i = 1; i <= m; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    int n1 = get_num(x, c), n2 = get_num(y, c);
    ins(x, n1, 1);
    ins(n1, x, 1);
    ins(n1, y, 1);
    ins(y, n1, 1);
    ins(n1, n2, 0);
    ins(n2, n1, 0);
  }
  spfa();
}