Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 7;

class CompareDist {
public:
  bool operator()(pair<int, int> n1, pair<int, int> n2) {
    return n1.second < n2.second;
  }
};

class Dijkstra { // O((N+M)logN)
private:
  static const int INF = 1e9 + 7;
  vector<vector<pair<long long, int>>> graph; // pair<cost,next_path>
  vector<long long> total_cost;
  vector<vector<int>> shortest_path;

public:
  Dijkstra(vector<vector<pair<long long, int>>> graph, int start, int size,
           bool path_check = false) {
    priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                   // greater<pair<long long,int>>> q;
                   CompareDist>
        q;
    vector<long long> cost(size, 1e18 + 7);
    vector<vector<int>> path(size);
    if (path_check)
      for (int i = 0; i < size; i++)
        path[i].push_back(i); // 自分は必ず通る
    cost[start] = 0;
    q.push(make_pair(0ll, start));
    while (!q.empty()) {
      auto now = q.top();
      q.pop();
      int prev = now.second;
      if (cost[prev] < now.first)
        continue;
      for (auto next : graph[prev]) {
        if (cost[next.second] <= cost[now.second] + next.first)
          continue;
        cost[next.second] = cost[now.second] + next.first;
        // 先頭からpathを追加
        // if(path_check)
        // path[next.second].insert(path[next.second].begin(),path[now.second].begin(),path[now.second].end());
        q.push(next);
      }
    }
    total_cost = cost;
    if (path_check)
      shortest_path = path;
  }
  vector<long long> get_cost() { return total_cost; }
  vector<vector<int>> get_path() { return shortest_path; }
};
#define ll long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  // 長さ、次の場所のペア
  vector<vector<pair<ll, int>>> graph(n);
  rep(i, 0, m) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    graph[l].push_back(make_pair(c, r));
  }
  rep(i, 1, n) graph[i].push_back(make_pair(0, i - 1));

  Dijkstra g(graph, 0, n);
  if (g.get_cost()[n - 1] == inf)
    cout << -1 << endl;
  else
    cout << g.get_cost()[n - 1] << endl;
  /*
  vector<ll> ans = Dijk(graph,0,n);
  if(ans[n-1] == inf) cout << -1 << endl;
  else cout << ans[n-1] << endl;
  */
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 7;

class CompareDist {
public:
  bool operator()(pair<int, int> n1, pair<int, int> n2) {
    return n1.second > n2.second;
  }
};

class Dijkstra { // O((N+M)logN)
private:
  static const int INF = 1e9 + 7;
  vector<vector<pair<long long, int>>> graph; // pair<cost,next_path>
  vector<long long> total_cost;
  vector<vector<int>> shortest_path;

public:
  Dijkstra(vector<vector<pair<long long, int>>> graph, int start, int size,
           bool path_check = false) {
    priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                   // greater<pair<long long,int>>> q;
                   CompareDist>
        q;
    vector<long long> cost(size, 1e18 + 7);
    vector<vector<int>> path(size);
    if (path_check)
      for (int i = 0; i < size; i++)
        path[i].push_back(i); // 自分は必ず通る
    cost[start] = 0;
    q.push(make_pair(0ll, start));
    while (!q.empty()) {
      auto now = q.top();
      q.pop();
      int prev = now.second;
      if (cost[prev] < now.first)
        continue;
      for (auto next : graph[prev]) {
        if (cost[next.second] <= cost[now.second] + next.first)
          continue;
        cost[next.second] = cost[now.second] + next.first;
        // 先頭からpathを追加
        // if(path_check)
        // path[next.second].insert(path[next.second].begin(),path[now.second].begin(),path[now.second].end());
        q.push(next);
      }
    }
    total_cost = cost;
    if (path_check)
      shortest_path = path;
  }
  vector<long long> get_cost() { return total_cost; }
  vector<vector<int>> get_path() { return shortest_path; }
};
#define ll long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  // 長さ、次の場所のペア
  vector<vector<pair<ll, int>>> graph(n);
  rep(i, 0, m) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    graph[l].push_back(make_pair(c, r));
  }
  rep(i, 1, n) graph[i].push_back(make_pair(0, i - 1));

  Dijkstra g(graph, 0, n);
  if (g.get_cost()[n - 1] == inf)
    cout << -1 << endl;
  else
    cout << g.get_cost()[n - 1] << endl;
  /*
  vector<ll> ans = Dijk(graph,0,n);
  if(ans[n-1] == inf) cout << -1 << endl;
  else cout << ans[n-1] << endl;
  */
}