Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame                                                                   \
  {                                                                            \
    puts("-1");                                                                \
    return 0;                                                                  \
  }
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
struct edge {
  int to, num;
};
const int INF = 1001001001;
const int MAX_V = 200005;
int V, E;
vector<edge> G[MAX_V];
int d[MAX_V];
set<int> se[MAX_V];
int f[MAX_V];

void dijkstra(int s) {
  // greater<P>を指定することでfirstが小さい順に取り出せるようにする
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] > d[V - 1])
      continue;
    if (f[v] == 1)
      continue;
    f[v] = 1;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] >= d[v]) {
        auto it = se[v].find(e.num);
        if (it != se[v].end()) {
          if (d[e.to] == d[v]) {
            auto it2 = se[e.to].find(e.num);
            if (it2 == se[e.to].end()) {
              se[e.to].insert(e.num);
              f[e.to] = 0;
              que.push(P(d[e.to], e.to));
            }
          } else {
            se[e.to].clear();
            se[e.to].insert(e.num);
            d[e.to] = d[v];
            f[e.to] = 0;
            que.push(P(d[e.to], e.to));
          }
        } else if (d[e.to] > d[v]) {
          if (d[e.to] == d[v] + 1) {
            auto it2 = se[e.to].find(e.num);
            if (it2 == se[e.to].end()) {
              se[e.to].insert(e.num);
              f[e.to] = 0;
              que.push(P(d[e.to], e.to));
            }
          } else {
            se[e.to].clear();
            se[e.to].insert(e.num);
            d[e.to] = d[v] + 1;
            f[e.to] = 0;
            que.push(P(d[e.to], e.to));
          }
        }
      }
    }
  }
}

int main() {
  cin >> V >> E;
  rep(i, E) {
    int from, to, num;
    cin >> from >> to >> num;
    from--;
    to--;
    edge e1, e2;
    e1.to = to;
    e1.num = num;
    e2.to = from;
    e2.num = num;
    G[from].push_back(e1);
    G[to].push_back(e2);
  }

  dijkstra(0);

  if (d[V - 1] == INF)
    d[V - 1] = -1;
  cout << d[V - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame                                                                   \
  {                                                                            \
    puts("-1");                                                                \
    return 0;                                                                  \
  }
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
struct edge {
  int to, num;
};
const int INF = 1001001001;
const int MAX_V = 200005;
int V, E;
vector<edge> G[MAX_V];
int d[MAX_V];
set<int> se[MAX_V];
int f[MAX_V];

void dijkstra(int s) {
  // greater<P>を指定することでfirstが小さい順に取り出せるようにする
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] >= d[V - 1])
      continue;
    if (f[v] == 1)
      continue;
    f[v] = 1;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] >= d[v]) {
        auto it = se[v].find(e.num);
        if (it != se[v].end()) {
          if (d[e.to] == d[v]) {
            auto it2 = se[e.to].find(e.num);
            if (it2 == se[e.to].end()) {
              se[e.to].insert(e.num);
              f[e.to] = 0;
              que.push(P(d[e.to], e.to));
            }
          } else {
            se[e.to].clear();
            se[e.to].insert(e.num);
            d[e.to] = d[v];
            f[e.to] = 0;
            que.push(P(d[e.to], e.to));
          }
        } else if (d[e.to] > d[v]) {
          if (d[e.to] == d[v] + 1) {
            auto it2 = se[e.to].find(e.num);
            if (it2 == se[e.to].end()) {
              se[e.to].insert(e.num);
              f[e.to] = 0;
              que.push(P(d[e.to], e.to));
            }
          } else {
            se[e.to].clear();
            se[e.to].insert(e.num);
            d[e.to] = d[v] + 1;
            f[e.to] = 0;
            que.push(P(d[e.to], e.to));
          }
        }
      }
    }
  }
}

int main() {
  cin >> V >> E;
  rep(i, E) {
    int from, to, num;
    cin >> from >> to >> num;
    from--;
    to--;
    edge e1, e2;
    e1.to = to;
    e1.num = num;
    e2.to = from;
    e2.num = num;
    G[from].push_back(e1);
    G[to].push_back(e2);
  }

  dijkstra(0);

  if (d[V - 1] == INF)
    d[V - 1] = -1;
  cout << d[V - 1] << endl;
  return 0;
}