Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "stdio.h"
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using Pii = pair<int, int>;

const int inf = 1e9 + 7;
const ll longinf = 1LL << 50;
const long long mod_const = 1e9 + 7;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

using MIte = multimap<ll, ll, greater<ll>>::iterator;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using pll = pair<long long, long long>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll N, M;
  cin >> N >> M;

  vector<pair<ll, pll>> vpath(M);
  rep(i, M) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    vpath[i] = make_pair(l, make_pair(r, c));
  }

  sort(vpath.begin(), vpath.end());

  vector<ll> vLen(N, longinf);
  vLen[0] = 0;
  rep(i, M) {
    ll start = vpath[i].first;
    ll end = vpath[i].second.first;
    ll d = vpath[i].second.second;

    if (start == 0) {
      for (int j = start + 1; j <= end; ++j) {
        vLen[j] = min(vLen[j], vLen[start] + d);
      }
    } else {
      if (vLen[start] != longinf) {
        for (int j = start + 1; j <= end; ++j) {
          vLen[j] = min(vLen[j], vLen[start] + d);
        }
      }
    }
  }

  if (vLen[N - 1] == longinf) {
    cout << "-1" << endl;
  } else {
    cout << vLen[N - 1] << endl;
  }

  return 0;
}


Fixed submission:
#include "stdio.h"
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)

using ll = long long;
using Pii = pair<int, int>;

const int inf = 1e9 + 7;
const ll longinf = 1LL << 50;
const long long mod_const = 1e9 + 7;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

using MIte = multimap<ll, ll, greater<ll>>::iterator;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using pll = pair<long long, long long>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll N, M;
  cin >> N >> M;

  vector<pair<ll, pll>> vpath(M);
  rep(i, M) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    vpath[i] = make_pair(l, make_pair(r, c));
  }

  sort(vpath.begin(), vpath.end());

  vector<ll> vLen(N, longinf);
  vLen[0] = 0;
  rep(i, M) {
    ll start = vpath[i].first;
    ll end = vpath[i].second.first;
    ll d = vpath[i].second.second;

    if (start == 0) {
      for (int j = start + 1; j <= end; ++j) {
        vLen[j] = min(vLen[j], vLen[start] + d);
      }
    } else {
      if (vLen[start] != longinf) {
        if (vLen[start] + d <= vLen[end]) {
          for (int j = start + 1; j <= end; ++j) {
            vLen[j] = min(vLen[j], vLen[start] + d);
          }
        }
      }
    }
  }

  if (vLen[N - 1] == longinf) {
    cout << "-1" << endl;
  } else {
    cout << vLen[N - 1] << endl;
  }

  return 0;
}