Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int n, m;
vector<pair<int, int>> v[100005];
map<int, int> d[100005];
set<pair<int, pair<int, int>>> s;
int cnt[100005];

inline bool cmp(pair<int, int> x, pair<int, int> y) {
  return x.second < y.second;
}

int main(int argc, const char *argv[]) {
#ifdef __APPLE__
  freopen("/Users/danya.smelskiy/Documents/Danya/Resources/input.txt", "r",
          stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
    if (x > y)
      swap(x, y);
    d[x][z] = 1e9;
    d[y][z] = 1e9;
  }
  for (int i = 0; i < (int)v[1].size(); ++i) {
    int to = v[1][i].first;
    int x = v[1][i].second;
    if (d[to][x] == 1e9) {
      s.insert({1, {to, x}});
      d[to][x] = 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    sort(v[i].begin(), v[i].end(), cmp);
  }
  while (!s.empty()) {
    pair<int, int> cur = s.begin()->second;
    s.erase(s.begin());
    int x = cur.first;
    int y = cur.second;
    int len = d[x][y];
    if (cnt[x] == 2) {
      int l = 0;
      int r = (int)v[x].size() - 1;
      while (l < r) {
        int mid = (l + r) >> 1;
        if (v[x][mid].second >= y)
          r = mid;
        else
          l = mid + 1;
      }
      while (l < (int)v[x].size() && v[x][l].second == y) {
        int to = v[x][l].first;
        int r = len;
        if (r < d[to][y]) {
          s.erase({d[to][y], {to, y}});
          d[to][y] = r;
          s.insert({d[to][y], {to, y}});
        }
        ++l;
      }
    }
    ++cnt[x];
    for (int i = 0; i < (int)v[x].size(); ++i) {
      int to = v[x][i].first;
      int l = v[x][i].second;
      int r = len + (l == y ? 0 : 1);
      if (r < d[to][l]) {
        s.erase({d[to][l], {to, l}});
        d[to][l] = r;
        s.insert({d[to][l], {to, l}});
      }
    }
  }
  int res = 1e9;
  for (auto t : d[n]) {
    res = min(res, t.second);
  }
  cout << (res == 1e9 ? -1 : res) << '\n';
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int n, m;
vector<pair<int, int>> v[100005];
map<int, int> d[100005];
set<pair<int, pair<int, int>>> s;
int cnt[100005];

inline bool cmp(pair<int, int> x, pair<int, int> y) {
  return x.second < y.second;
}

int main(int argc, const char *argv[]) {
#ifdef __APPLE__
  freopen("/Users/danya.smelskiy/Documents/Danya/Resources/input.txt", "r",
          stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
    if (x > y)
      swap(x, y);
    d[x][z] = 1e9;
    d[y][z] = 1e9;
  }
  for (int i = 0; i < (int)v[1].size(); ++i) {
    int to = v[1][i].first;
    int x = v[1][i].second;
    if (d[to][x] == 1e9) {
      s.insert({1, {to, x}});
      d[to][x] = 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    sort(v[i].begin(), v[i].end(), cmp);
  }
  while (!s.empty()) {
    pair<int, int> cur = s.begin()->second;
    s.erase(s.begin());
    int x = cur.first;
    int y = cur.second;
    int len = d[x][y];
    if (cnt[x] == 2) {
      int l = 0;
      int r = (int)v[x].size() - 1;
      while (l < r) {
        int mid = (l + r) >> 1;
        if (v[x][mid].second >= y)
          r = mid;
        else
          l = mid + 1;
      }
      while (l < (int)v[x].size() && v[x][l].second == y) {
        int to = v[x][l].first;
        int r = len;
        if (r < d[to][y]) {
          s.erase({d[to][y], {to, y}});
          d[to][y] = r;
          s.insert({d[to][y], {to, y}});
        }
        ++l;
      }
      continue;
    }
    ++cnt[x];
    for (int i = 0; i < (int)v[x].size(); ++i) {
      int to = v[x][i].first;
      int l = v[x][i].second;
      int r = len + (l == y ? 0 : 1);
      if (r < d[to][l]) {
        s.erase({d[to][l], {to, l}});
        d[to][l] = r;
        s.insert({d[to][l], {to, l}});
      }
    }
  }
  int res = 1e9;
  for (auto t : d[n]) {
    res = min(res, t.second);
  }
  cout << (res == 1e9 ? -1 : res) << '\n';
}