Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define INF_LL 100000000000000000LL
#define INF 200000000
#define MOD 998244353
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// typedef float double;
// typedef priority_queue prique;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll modpow(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll t = modpow(a, b / 2, m);
  if (b & 1) {
    return (t * t % m) * a % m;
  } else {
    return t * t % m;
  }
}

struct edge {
  int to;
  ll cost;
  edge(int t, ll c) { to = t, cost = c; }
};

vector<ll> dijkstra(vector<vector<edge>> &g, int start, int n) {
  priority_queue<P, vector<P>, greater<P>> que;
  vector<ll> dist(n, INF_LL);
  dist[start] = 0;
  que.push(P(0, start));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (edge e : g[v]) {
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
  return dist;
}

signed main() {
  int n, m;
  cin >> n >> m;
  vector<vector<edge>> g(n);
  rep(i, n - 1) { g[i + 1].push_back({i, 0}); }
  rep(i, m) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    g[l - 1].push_back({r - 1, c});
  }
  vl dist = dijkstra(g, 0, n);
  if (dist[n - 1] == INF_LL) {
    cout << -1 << endl;
  } else {
    cout << dist[n - 1] << endl;
  }
  return 0;
}


Fixed submission:
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define INF_LL 100000000000000000LL
#define INF 200000000
#define MOD 998244353
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// typedef float double;
// typedef priority_queue prique;
typedef pair<ll, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll modpow(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll t = modpow(a, b / 2, m);
  if (b & 1) {
    return (t * t % m) * a % m;
  } else {
    return t * t % m;
  }
}

struct edge {
  int to;
  ll cost;
  edge(int t, ll c) { to = t, cost = c; }
};

vector<ll> dijkstra(vector<vector<edge>> &g, int start, int n) {
  priority_queue<P, vector<P>, greater<P>> que;
  vector<ll> dist(n, INF_LL);
  dist[start] = 0;
  que.push(P(0, start));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (edge e : g[v]) {
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(P(dist[e.to], e.to));
      }
    }
  }
  return dist;
}

signed main() {
  int n, m;
  cin >> n >> m;
  vector<vector<edge>> g(n);
  rep(i, n - 1) { g[i + 1].push_back({i, 0}); }
  rep(i, m) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    g[l - 1].push_back({r - 1, c});
  }
  vl dist = dijkstra(g, 0, n);
  if (dist[n - 1] == INF_LL) {
    cout << -1 << endl;
  } else {
    cout << dist[n - 1] << endl;
  }
  return 0;
}