Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<pair<int, int>, ll>> LRC(N);
  rep(i, M) {
    cin >> LRC[i].first.first >> LRC[i].first.second >> LRC[i].second;
    LRC[i].first.first--;
    LRC[i].first.second--;
  }
  vector<vector<int>> G(N);
  vector<vector<ll>> CO(N);
  rep(i, M) {
    G[LRC[i].first.first].pb(LRC[i].first.second);
    CO[LRC[i].first.first].pb(LRC[i].second);
  }
  rep(i, N - 1) {
    G[i + 1].pb(i);
    CO[i + 1].pb(0);
  }

  // ここからダイクストラ
  vector<ll> D(N, -1);
  vector<bool> used(N, false);
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      q;

  q.push({0, 0});
  int cnt = 0;
  while (!q.empty()) {
    pair<ll, int> nowNode = q.top();
    q.pop();
    if (used[nowNode.second])
      continue;
    used[nowNode.second] = true;
    D[nowNode.second] = nowNode.first;
    rep(i, (int)G[nowNode.second].size()) {
      // if(D[G[nowNode.second][i]] != -1 && D[G[nowNode.second][i]] <=
      // D[nowNode.second] + nowNode.first) continue;
      q.push({D[nowNode.second] + CO[nowNode.second][i], G[nowNode.second][i]});
      cnt++;
    }
  }
  // rep(i,N) cout << D[i] << endl;
  // cout << cnt << endl;
  cout << D[N - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<pair<int, int>, ll>> LRC(M);
  rep(i, M) {
    cin >> LRC[i].first.first >> LRC[i].first.second >> LRC[i].second;
    LRC[i].first.first--;
    LRC[i].first.second--;
  }
  vector<vector<int>> G(N);
  vector<vector<ll>> CO(N);
  rep(i, M) {
    G[LRC[i].first.first].pb(LRC[i].first.second);
    CO[LRC[i].first.first].pb(LRC[i].second);
  }
  rep(i, N - 1) {
    G[i + 1].pb(i);
    CO[i + 1].pb(0);
  }

  // ここからダイクストラ
  vector<ll> D(N, -1);
  vector<bool> used(N, false);
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      q;

  q.push({0, 0});
  int cnt = 0;
  while (!q.empty()) {
    pair<ll, int> nowNode = q.top();
    q.pop();
    if (used[nowNode.second])
      continue;
    used[nowNode.second] = true;
    D[nowNode.second] = nowNode.first;
    rep(i, (int)G[nowNode.second].size()) {
      // if(D[G[nowNode.second][i]] != -1 && D[G[nowNode.second][i]] <=
      // D[nowNode.second] + nowNode.first) continue;
      q.push({D[nowNode.second] + CO[nowNode.second][i], G[nowNode.second][i]});
      cnt++;
    }
  }
  // rep(i,N) cout << D[i] << endl;
  // cout << cnt << endl;
  cout << D[N - 1] << endl;
  return 0;
}