Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 4e5 + 100;
using ll = long long;
const int M = 1e9 + 7;
vector<pair<int, int>> v1[maxn], v3[maxn], v2[maxn];
int tot = 0;
int vis[maxn];
void dij(int st, int en) {
  static int dis[maxn];
  for (int i = 0; i <= tot; ++i)
    dis[i] = inf, vis[i] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  dis[st] = 0;
  q.push({0, st});
  while (!q.empty()) {
    pair<int, int> tp = q.top();
    q.pop();
    vis[tp.second] = 0;
    for (auto &u : v3[tp.second]) {
      if (dis[u.first] > dis[tp.second] + u.second) {
        dis[u.first] = dis[tp.second] + u.second;
        if (!vis[u.first])
          vis[u.first] = 1, q.push({dis[u.first], u.first});
      }
    }
  }
  int ans = dis[en];
  if (ans == inf)
    ans = -2;
  printf("%d\n", ans / 2);
}
int main() {
  //  freopen("1.in","r",stdin);
  //   freopen("1.out","w",stdout);
  int n, m;
  //  clock_t c1 = clock(),c2;int mx = 0;
  while (~scanf("%d%d", &n, &m)) {
    tot = 0;
    for (int i = 1; i <= m; ++i) {
      int x, y, c;
      scanf("%d %d %d", &x, &y, &c);
      v1[x].push_back({y, c});
      v2[x].push_back({c, 0});
      v2[y].push_back({c, 0});
    }
    tot = n;
    for (int i = 1; i <= n; ++i) {
      sort(v2[i].begin(), v2[i].end());
      v2[i].erase(unique(v2[i].begin(), v2[i].end()), v2[i].end());
      for (auto &u : v2[i])
        u.second = ++tot;
    }
    for (int i = 1; i <= n; ++i) {
      for (auto &u : v2[i]) {
        v3[u.second].push_back({i, 1});
        v3[i].push_back({u.second, 1});
      }
      for (auto &u : v1[i]) {
        int x = u.first, c = u.second;
        int p = lower_bound(v2[x].begin(), v2[x].end(), make_pair(c, 0)) -
                v2[x].begin();
        p = v2[x][p].second;
        int p2 = lower_bound(v2[i].begin(), v2[i].end(), make_pair(c, 0)) -
                 v2[i].begin();
        p2 = v2[i][p2].second;
        v3[p].push_back({p2, 0});
        v3[p2].push_back({p, 0});
      }
    }
    dij(1, n);
    for (int i = 0; i <= tot; ++i) {
      v1[i].clear();
      v2[i].clear();
      v3[i].clear();
    }
    //      mx = max(mx,tot);
  }
  // c2 = clock();
  // cout<<(c2-c1)/1000.0<<" "<<mx<<endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 5e5 + 100;
using ll = long long;
const int M = 1e9 + 7;
vector<pair<int, int>> v1[maxn], v3[maxn], v2[maxn];
int tot = 0;
int vis[maxn];
void dij(int st, int en) {
  static int dis[maxn];
  for (int i = 0; i <= tot; ++i)
    dis[i] = inf, vis[i] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  dis[st] = 0;
  q.push({0, st});
  while (!q.empty()) {
    pair<int, int> tp = q.top();
    q.pop();
    vis[tp.second] = 0;
    for (auto &u : v3[tp.second]) {
      if (dis[u.first] > dis[tp.second] + u.second) {
        dis[u.first] = dis[tp.second] + u.second;
        if (!vis[u.first])
          vis[u.first] = 1, q.push({dis[u.first], u.first});
      }
    }
  }
  int ans = dis[en];
  if (ans == inf)
    ans = -2;
  printf("%d\n", ans / 2);
}
int main() {
  //  freopen("1.in","r",stdin);
  //   freopen("1.out","w",stdout);
  int n, m;
  //  clock_t c1 = clock(),c2;int mx = 0;
  while (~scanf("%d%d", &n, &m)) {
    tot = 0;
    for (int i = 1; i <= m; ++i) {
      int x, y, c;
      scanf("%d %d %d", &x, &y, &c);
      v1[x].push_back({y, c});
      v2[x].push_back({c, 0});
      v2[y].push_back({c, 0});
    }
    tot = n;
    for (int i = 1; i <= n; ++i) {
      sort(v2[i].begin(), v2[i].end());
      v2[i].erase(unique(v2[i].begin(), v2[i].end()), v2[i].end());
      for (auto &u : v2[i])
        u.second = ++tot;
    }
    for (int i = 1; i <= n; ++i) {
      for (auto &u : v2[i]) {
        v3[u.second].push_back({i, 1});
        v3[i].push_back({u.second, 1});
      }
      for (auto &u : v1[i]) {
        int x = u.first, c = u.second;
        int p = lower_bound(v2[x].begin(), v2[x].end(), make_pair(c, 0)) -
                v2[x].begin();
        p = v2[x][p].second;
        int p2 = lower_bound(v2[i].begin(), v2[i].end(), make_pair(c, 0)) -
                 v2[i].begin();
        p2 = v2[i][p2].second;
        v3[p].push_back({p2, 0});
        v3[p2].push_back({p, 0});
      }
    }
    dij(1, n);
    for (int i = 0; i <= tot; ++i) {
      v1[i].clear();
      v2[i].clear();
      v3[i].clear();
    }
    //      mx = max(mx,tot);
  }
  // c2 = clock();
  // cout<<(c2-c1)/1000.0<<" "<<mx<<endl;
  return 0;
}