Problem p01075 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x) < EPS)
#define EQ(a, b) (abs(a - b) < EPS)
typedef complex<Real> P;

int n, m;
int dist[125252];
vector<pii> g[125252];
bool used[125252];

const int INF = 1145141919;

int main() {
  scanf("%d%d", &n, &m);
  REP(i, m) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    --a;
    --b;
    g[a].push_back(pii(b, c));
  }
  // dijkstra in 0~n-2
  REP(i, n) dist[i] = INF;
  priority_queue<pii> Q;
  Q.push(pii(-0, 0));
  dist[0] = 0;
  int ans = -1;
  while (!Q.empty()) {
    pii P = Q.top();
    Q.pop();
    int d = -P.first;
    int pos = P.second;
    if (used[pos])
      continue;
    used[pos] = true;
    REP(i, g[pos].size()) {
      int to = g[pos][i].first;
      int c = g[pos][i].second;
      if (c < d)
        continue;
      if (to == n - 1) {
        CHMAX(ans, c);
      } else {
        if (dist[to] <= c)
          continue;
        dist[to] = c;
        Q.push(pii(-c, to));
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x) < EPS)
#define EQ(a, b) (abs(a - b) < EPS)
typedef complex<Real> P;

int n, m;
int dist[125252];
vector<pii> g[125252];

const int INF = 830252521;

int main() {
  scanf("%d%d", &n, &m);
  REP(i, m) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    --a;
    --b;
    g[a].push_back(pii(b, c));
    g[b].push_back(pii(a, c));
  }
  // dijkstra in 0~n-2
  REP(i, n) dist[i] = INF;
  priority_queue<pii> Q;
  Q.push(pii(-0, 0));
  dist[0] = 0;
  while (!Q.empty()) {
    pii P = Q.top();
    Q.pop();
    int d = -P.first;
    int pos = P.second;
    if (dist[pos] < d)
      continue;
    REP(i, g[pos].size()) {
      int to = g[pos][i].first;
      if (to == n - 1)
        continue;
      int c = g[pos][i].second;
      if (c < d)
        continue;
      if (c > dist[to])
        continue;
      dist[to] = c;
      Q.push(pii(-c, to));
    }
  }
  int ans = -1;
  REP(i, n) {
    REP(j, g[i].size()) {
      int to = g[i][j].first;
      if (to != n - 1)
        continue;
      int c = g[i][j].second;
      if (c >= dist[i]) {
        CHMAX(ans, c);
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}