Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define PB(e) push_back(e)
#define FOREACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define BIT(n, m) (((n) >> (m)) & 1)

typedef long long ll;
typedef pair<int, int> PI;

template <typename S, typename T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()) {
    if (i > 0)
      out << ", ";
    out << v[i];
  }
  out << "]";
  return out;
}

const ll INF = 1e18 + 7;

int main() {
  int N, M;
  scanf("%d %d", &N, &M);

  vector<vector<PI>> G(N + 1);
  vector<ll> D(N + 1, INF);

  REP(i, M) {
    int l, r, c;
    scanf("%d %d %d", &l, &r, &c);
    G[l].push_back(PI(r, c));
  }

  REP2(i, 1, N) { G[i + 1].push_back(PI(i, 0)); }

  D[1] = 0;
  priority_queue<PI, vector<PI>, greater<PI>> que;
  que.push(PI(0, 1));

  while (!que.empty()) {
    PI p = que.top();
    que.pop();
    // cout << p << endl;
    if (p.second == N) {
      break;
    }

    if (p.first > D[p.second]) {
      continue;
    }

    for (const auto &w : G[p.second]) {
      if (D[w.first] > D[p.second] + w.second) {
        D[w.first] = D[p.second] + w.second;
        que.push(PI(D[w.first], w.first));
      }
    }
  }

  if (D[N] < INF) {
    cout << D[N] << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REPD(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define PB(e) push_back(e)
#define FOREACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define BIT(n, m) (((n) >> (m)) & 1)

typedef long long ll;
typedef pair<ll, ll> PI;

template <typename S, typename T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()) {
    if (i > 0)
      out << ", ";
    out << v[i];
  }
  out << "]";
  return out;
}

const ll INF = 1e18 + 7;

int main() {
  int N, M;
  scanf("%d %d", &N, &M);

  vector<vector<PI>> G(N + 1);
  vector<ll> D(N + 1, INF);

  REP(i, M) {
    int l, r, c;
    scanf("%d %d %d", &l, &r, &c);
    G[l].push_back(PI(r, c));
  }

  REP2(i, 1, N) { G[i + 1].push_back(PI(i, 0)); }

  D[1] = 0;
  priority_queue<PI, vector<PI>, greater<PI>> que;
  que.push(PI(0, 1));

  while (!que.empty()) {
    PI p = que.top();
    que.pop();
    // cout << p << endl;
    if (p.second == N) {
      break;
    }

    if (p.first > D[p.second]) {
      continue;
    }

    for (const auto &w : G[p.second]) {
      if (D[w.first] > D[p.second] + w.second) {
        D[w.first] = D[p.second] + w.second;
        que.push(PI(D[w.first], w.first));
      }
    }
  }

  if (D[N] < INF) {
    cout << D[N] << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}