alexjercan/bugnet huggingface dataset:
Problem p01064 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  int n;
  cin >> n;
  int a, d;
  cin >> a >> d;
  int m;
  cin >> m;
  vvi in(m, vi(3));
  rep(i, m) rep(j, 3) cin >> in[i][j];
  int q;
  cin >> q;

  for (int i = m - 1; i >= 0; i--)
    if (in[i][0] == 0 && in[i][1] <= q && q <= in[i][2]) {
      q = in[i][2] - q + in[i][1];
    }
  int out = a + d * (q - 1);
  rep(i, m) {
    if (in[i][0] == 1) {
      if (in[i][1] <= q && q <= in[i][2])
        out++;
    } else if (in[i][0] == 2) {
      if (in[i][1] <= q && q <= in[i][2])
        out /= 2;
    } else if (in[i][1] <= q && q <= in[i][2]) {
      q = in[i][2] - q + in[i][1];
    }
  }
  cout << out << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  int n;
  cin >> n;
  int a, d;
  cin >> a >> d;
  int m;
  cin >> m;
  vvi in(n, vi(3));
  rep(i, m) rep(j, 3) cin >> in[i][j];
  int q;
  cin >> q;

  for (int i = m - 1; i >= 0; i--)
    if (in[i][0] == 0 && in[i][1] <= q && q <= in[i][2]) {
      q = in[i][2] - q + in[i][1];
    }
  int out = a + d * (q - 1);
  rep(i, m) {
    if (in[i][0] == 1) {
      if (in[i][1] <= q && q <= in[i][2])
        out++;
    } else if (in[i][0] == 2) {
      if (in[i][1] <= q && q <= in[i][2])
        out /= 2;
    } else if (in[i][1] <= q && q <= in[i][2]) {
      q = in[i][2] - q + in[i][1];
    }
  }
  cout << out << endl;
}
Problem p01065 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dp[2][1200][1200];
int main() {
  memset(dp, -1, sizeof(dp));
  int x, y, n;
  cin >> n >> x >> y;
  dp[0][x + 600][y + 600] = 0;
  r(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    r(A, 1200) r(B, 1200) {
      if (~dp[i % 2][A][B]) {
        dp[(i + 1) % 2][A][B] = max(dp[(i + 1) % 2][A][B], dp[i % 2][A][B]);
        dp[(i + 1) % 2][A - a][B + b] =
            max(dp[(i + 1) % 2][A - a][B + b], dp[i % 2][A][B]);
        dp[(i + 1) % 2][A][B - c] =
            max(dp[(i + 1) % 2][A][B - c], dp[i % 2][A][B] + d);
      }
    }
    r(j, 1200) r(k, 1200) dp[i % 2][j][k] = -1;
  }
  int ans = 0;
  r(i, 1200) r(j, 1200) {
    if (i >= 600 && j >= 600) {
      ans = max(ans, dp[n % 2][i][j]);
    }
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dp[101][1200][1200];
int main() {
  memset(dp, -1, sizeof(dp));
  int x, y, n;
  cin >> n >> x >> y;
  dp[0][x + 600][y + 600] = 0;
  r(i, n) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    r(A, 1200) r(B, 1200) {
      if (~dp[i][A][B]) {
        dp[i + 1][A - a][B + b] = max(dp[i + 1][A - a][B + b], dp[i][A][B]);
        dp[i + 1][A][B - c] = max(dp[i + 1][A][B - c], dp[i][A][B] + d);
      }
    }
  }
  int ans = 0;
  r(i, 1200) r(j, 1200) {
    if (i >= 600 && j >= 600) {
      ans = max(ans, dp[n][i][j]);
    }
  }
  cout << ans << endl;
}
Problem p01065 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
using namespace std;

int dp[301][301][901];
struct exc {
  int a;
  int b;
  int c;
  int d;
};
int main() {
  int N;
  cin >> N;
  int X, Y;
  cin >> X >> Y;
  for (int i = 0; i < 301; ++i) {
    for (int j = 0; j < 301; ++j) {
      for (int k = 0; k < 901; ++k) {
        dp[i][j][k] = -1e9;
      }
    }
  }
  dp[0][X][Y + 300] = 0;
  vector<exc> es;
  for (int i = 0; i < N; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    es.push_back(exc{a, b, c, d});
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 301; ++j) {
      for (int k = 0; k < 901; ++k) {
        exc ex(es[i]);
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        {
          int nj = j - ex.a;
          int nk = k + ex.b;
          if (nj < 0) {

          } else {
            if (nk > 900)
              nk = 900;
            dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i][j][k]);
          }
        }
        {
          int nk = k - ex.c;
          if (nk < 0) {

          } else {
            dp[i + 1][j][nk] = max(dp[i + 1][j][nk], dp[i][j][k] + ex.d);
          }
        }
      }
    }
  }
  int ans = 0;
  for (int j = 0; j <= 300; ++j) {
    for (int k = 300; k <= 900; ++k) {
      ans = max(ans, dp[N][j][k]);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

int dp[301][601][901];
struct exc {
  int a;
  int b;
  int c;
  int d;
};
int main() {
  int N;
  cin >> N;
  int X, Y;
  cin >> X >> Y;
  dp[0][X][Y + 300] = 0;
  for (int i = 0; i < 301; ++i) {
    for (int j = 0; j < 601; ++j) {
      for (int k = 0; k < 901; ++k) {
        dp[i][j][k] = -1e9;
      }
    }
  }
  vector<exc> es;
  for (int i = 0; i < N; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    es.push_back(exc{a, b, c, d});
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 601; ++j) {
      for (int k = 0; k < 601; ++k) {
        exc ex(es[i]);
        {
          int nj = j - ex.a;
          int nk = ex.b;
        }
      }
    }
  }
  return 0;
}
Problem p01065 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, x, y, dp[2][666][666];

int main() {
  const int GETA = 333;
  rep(i, 666) rep(j, 666) dp[0][i][j] = -inf;
  dp[0][GETA][GETA] = 0;

  int cur = 0, next = 1;
  cin >> n >> x >> y;
  rep(i, n) {
    rep(k, 666) rep(j, 666) dp[next][k][j] = dp[cur][k][j];
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    rep(j, 666) rep(k, 666) if (dp[cur][j][k] >= 0) {
      dp[next][j - a][k + b] = max(dp[next][j - a][k + b], dp[cur][j][k]);
      dp[next][j][k - c] = max(dp[next][j][k - c], dp[cur][j][k] + d);
    }
    swap(cur, next);
  }
  int ans = 0;
  rep(i, 666) rep(j, 666) if (dp[cur][i][j] >= 0 && x + i - GETA >= 0 &&
                              y + j - GETA >= 0) ans = max(ans, dp[cur][i][j]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, x, y, dp[2][666][666];

int main() {
  const int GETA = 333;
  rep(i, 666) rep(j, 666) dp[0][i][j] = -inf;
  dp[0][GETA][GETA] = 0;

  int cur = 0, next = 1;
  cin >> n >> x >> y;
  rep(i, n) {
    rep(k, 666) rep(j, 666) dp[next][k][j] = -inf;
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    rep(j, 666) rep(k, 666) if (dp[cur][j][k] >= 0) {
      dp[next][j - a][k + b] = max(dp[next][j - a][k + b], dp[cur][j][k]);
      dp[next][j][k - c] = max(dp[next][j][k - c], dp[cur][j][k] + d);
    }
    swap(cur, next);
  }
  cerr << endl;

  int ans = 0;
  rep(i, 666) rep(j, 666) if (dp[cur][i][j] && x + i - GETA >= 0 &&
                              y + j - GETA >= 0) ans = max(ans, dp[cur][i][j]);
  cout << ans << endl;
  return 0;
}
Problem p01065 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

constexpr int MAXA = 350;
constexpr int MAXB = 700;
constexpr int OFFSET = 350;

void chmax(int &a, int b) { a = (a < b ? b : a); }

int main() {
  int n, x, y;
  std::cin >> n >> x >> y;

  std::vector<int> a(n), b(n), c(n), d(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  std::vector<std::vector<int>> dp(MAXA, std::vector<int>(MAXB, -1));
  dp[x][y + OFFSET] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < MAXA; j++) {
      for (int k = 0; k < MAXB; k++) {
        if (dp[j][k] == -1) {
          continue;
        }

        if (j - a[i] >= 0 && k + b[i] < MAXB) {
          chmax(dp[j - a[i]][k + b[i]], dp[j][k]);
        }

        if (k - c[i] >= 0) {
          chmax(dp[j][k - c[i]], dp[j][k] + d[i]);
        }
      }
    }
  }

  int ret = 0;
  for (int i = 0; i < MAXA; i++) {
    for (int j = OFFSET; j < MAXB; j++) {
      chmax(ret, dp[i][j]);
    }
  }

  std::cout << ret << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

constexpr int MAXA = 300;
constexpr int MAXB = 600;
constexpr int OFFSET = 300;

void chmax(int &a, int b) { a = (a < b ? b : a); }

int main() {
  int n, x, y;
  std::cin >> n >> x >> y;

  std::vector<int> a(n), b(n), c(n), d(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i] >> b[i] >> c[i] >> d[i];
  }

  std::vector<std::vector<int>> dp(MAXA, std::vector<int>(MAXB, -1));
  dp[x][y + OFFSET] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < MAXA; j++) {
      for (int k = 0; k < MAXB; k++) {
        if (dp[j][k] == -1) {
          continue;
        }

        if (j - a[i] >= 0 && k + b[i] < MAXB) {
          chmax(dp[j - a[i]][k + b[i]], dp[j][k]);
        }

        if (k - c[i] >= 0) {
          chmax(dp[j][k - c[i]], dp[j][k] + d[i]);
        }
      }
    }
  }

  int ret = 0;
  for (int i = 0; i < MAXA; i++) {
    for (int j = OFFSET; j < MAXB; j++) {
      chmax(ret, dp[i][j]);
    }
  }

  std::cout << ret << std::endl;

  return 0;
}
Problem p01065 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;
const int MAX_N = 110;
const int MAX_XY = 310;

int dp[MAX_N][2 * MAX_XY][2 * MAX_XY];
bool ok[MAX_N][2 * MAX_XY][2 * MAX_XY];
int N;
vector<tuple<int, int, int, int>> A;
int memo(int pos, int x, int y) {

  if (pos == N) {
    //    cout << x << " " << y << endl;
    if (x >= 305 and y >= 305) {
      return 0;
    }
    return -114514;
  }

  if (ok[pos][x][y]) {
    return dp[pos][x][y];
  }
  ok[pos][x][y] = true;

  int res;
  res = -1141514;
  int a, b, c, d;

  tie(a, b, c, d) = A[pos];
  res = max(res, memo(pos + 1, x, y));
  if (x - a >= 0) {
    res = max(res, memo(pos + 1, x - a, y + b));
  }
  if (y - c >= 0) {
    res = max(res, memo(pos + 1, x, y - c) + d);
  }

  dp[pos][x][y] = res;
  return res;
}
int x, y;

int main() {

  cin >> N;
  cin >> x >> y;
  memset(ok, false, sizeof(ok));
  for (int i = 0; i < N; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    A.push_back(make_tuple(a, b, c, d));
  }
  cout << memo(0, x + 305, y + 305) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;
const int MAX_N = 110;
const int MAX_XY = 310;

map<tuple<int, int, int>, int> dp;
int N;
vector<tuple<int, int, int, int>> A;
int memo(int pos, int x, int y) {

  if (pos == N) {
    //    cout << x << " " << y << endl;
    if (x >= 0 and y >= 0) {
      return 0;
    }
    return -114514;
  }

  if (dp.count(make_tuple(pos, x, y)) > 0) {
    return dp[make_tuple(pos, x, y)];
  }
  //  cout << pos << " " << x << " " << y << endl;
  int res;
  res = -114514;
  int a, b, c, d;

  tie(a, b, c, d) = A[pos];
  res = max(res, memo(pos + 1, x, y));
  res = max(res, memo(pos + 1, x - a, y + b));
  res = max(res, memo(pos + 1, x, y - c) + d);

  dp[make_tuple(pos, x, y)] = res;
  return res;
}
int x, y;
int main() {

  cin >> N;
  cin >> x >> y;
  for (int i = 0; i < N; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    A.push_back(make_tuple(a, b, c, d));
  }
  cout << memo(0, x, y) << endl;
  return 0;
}
Problem p01065 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int n, x, y;
int a[102], b[102], c[102], d[102];

// idx, umaka, fugashi
int dp[102][302][700];
const int offset = 301;

#define at(i, j, k) dp[i][j][k + offset]

int dfs(int i, int j, int k) {
  if (i == n)
    return 0;
  int ret = dfs(i + 1, j, k);
  if (j >= a[i])
    ret = max(ret, dfs(i + 1, j - a[i], k + b[i]));
  ret = max(ret, dfs(i + 1, j, k - c[i]) + d[i]);
  return ret;
}

int main() {
  cin >> n >> x >> y;
  rep(i, n) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  rep(i, 102) rep(j, 302) rep(k, 302 * 2) dp[i][j][k] = -1e8;
  at(0, x, y) = 0;

  rep(i, n) rep(j, 302) for (int k = -301; k <= 301; k++) {
    // cout<<i<<", "<<j<<", "<<k<<endl;
    chmax(at(i + 1, j, k), at(i, j, k));
    if (j - a[i] >= 0 && k + b[i] < 303 * 2)
      chmax(at(i + 1, j - a[i], k + b[i]), at(i, j, k));
    if (k - c[i] + offset >= 0)
      chmax(at(i + 1, j, k - c[i]), at(i, j, k) + d[i]);
  }
  int ans = -1e8;
  rep(i, n + 1) rep(j, 302) rep(k, 302) chmax(ans, at(i, j, k));
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int n, x, y;
int a[102], b[102], c[102], d[102];

// idx, umaka, fugashi
int dp[102][302][700];
const int offset = 301;

#define at(i, j, k) dp[i][j][k + offset]

int dfs(int i, int j, int k) {
  if (i == n)
    return 0;
  int ret = dfs(i + 1, j, k);
  if (j >= a[i])
    ret = max(ret, dfs(i + 1, j - a[i], k + b[i]));
  ret = max(ret, dfs(i + 1, j, k - c[i]) + d[i]);
  return ret;
}

int main() {
  cin >> n >> x >> y;
  rep(i, n) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  rep(i, 102) rep(j, 303) rep(k, 302 * 2) dp[i][j][k] = -1e8;
  at(0, x, y) = 0;

  rep(i, n) rep(j, 302) for (int k = -301; k <= 301; k++) {
    // cout<<i<<", "<<j<<", "<<k<<endl;
    chmax(at(i + 1, j, k), at(i, j, k));
    if (j - a[i] >= 0 && k + b[i] < 303 * 2)
      chmax(at(i + 1, j - a[i], k + b[i]), at(i, j, k));
    if (k - c[i] + offset >= 0)
      chmax(at(i + 1, j, k - c[i]), at(i, j, k) + d[i]);
  }
  int ans = -1e8;
  rep(i, n + 1) rep(j, 302) rep(k, 302) chmax(ans, at(i, j, k));
  cout << ans << endl;

  return 0;
}
Problem p01073 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

int n, m, k;
int a[15], b[15];

int idx[4];
ll ans = 0;
void dfs(int num) {
  if (num == k) {
    vector<pair<int, int>> v;
    rep(i, k) v.pb({a[idx[i]], b[idx[i]]});
    sort(all(v));
    reverse(all(v));

    int rem = m;
    ll t = 0;
    rep(i, k) {
      int A = v[i].fi, B = v[i].se;

      t += A * min(B, rem);
      rem -= min(B, rem);
    }
    t += rem;

    ans = max(ans, t);
    return;
  }

  rep(i, n) {
    bool ok = true;
    rep(j, num) ok &= (idx[j] != i);

    if (ok) {
      idx[num] = i;
      dfs(num + 1);
      idx[num] = -1;
    }
  }
}

int main() {
  cin >> n >> m >> k;
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];

  memset(idx, -1, sizeof(idx));
  dfs(0);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

int n, m, k;
int a[15], b[15];

ll dp[4][16][10010];
ll dfs(int num, int x, int rem) {
  if (num == k)
    return rem;
  if (dp[num][x][rem] >= 0)
    return dp[num][x][rem];

  ll ret = rem;
  for (int i = x; i < n; ++i) {
    rep(j, min(b[i], rem) + 1) ret =
        max(ret, dfs(num + 1, i + 1, rem - j) + (ll)a[i] * j);
  }

  return dp[num][x][rem] = ret;
}

int main() {
  cin >> n >> m >> k;
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];

  memset(dp, -1, sizeof(dp));
  cout << dfs(0, 0, m) << endl;
  ;
  return 0;
}
Problem p01075 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> edges(N);
  vector<int> memo(N - 1, 1e9 + 1e7);
  memo[0] = 0;

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back(make_pair(b, c));
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    auto atop(que.top());
    que.pop();
    const int now = atop.second;
    const int now_time = atop.first;
    if (memo[now] < now_time)
      continue;
    for (auto e : edges[now]) {
      if (e.first == N - 1)
        continue;
      if (e.second >= now_time) {
        if (memo[e.first] > e.second) {
          que.push(make_pair(e.second, e.first));
          memo[e.first] = e.second;
        }
      }
    }
  }

  int ans = -1;
  for (int i = 0; i < N - 1; ++i) {
    for (auto e : edges[i]) {
      if (e.first == N - 1 && memo[i] <= e.second) {
        ans = max(ans, e.second);
      }
    }
  }
  { cout << ans << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> edges(N);
  vector<int> memo(N - 1, 1e9 + 1e7);
  memo[0] = 0;

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back(make_pair(b, c));
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    auto atop(que.top());
    que.pop();
    const int now = atop.second;
    const int now_time = atop.first;
    for (auto e : edges[now]) {
      if (e.first == N - 1)
        continue;
      if (e.second >= now_time) {
        if (memo[e.first] > e.second) {
          que.push(make_pair(e.second, e.first));
          memo[e.first] = e.second;
        }
      }
    }
  }

  int ans = -1;
  for (int i = 0; i < N - 1; ++i) {
    for (auto e : edges[i]) {
      if (e.first == N - 1 && memo[i] <= e.second) {
        ans = max(ans, e.second);
      }
    }
  }
  { cout << ans << endl; }
  return 0;
}
Problem p01075 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

int read() {
  int i;
  scanf("%d", &i);
  return i;
}

struct Edge {
  int a, b;
};

bool used[114514], cur[114514];
vi g[114514];

int main() {
  int n = read(), m = read();
  map<int, vector<Edge>> es;
  REP(i, m) {
    int a = read() - 1, b = read() - 1, t = read();
    es[t].PB(Edge{a, b});
  }
  int ans = -1;
  cur[0] = true;
  for (auto kv : es) {
    queue<int> q;
    for (auto e : kv.second) {
      g[e.a].PB(e.b);
      if (cur[e.a] && !used[e.a]) {
        used[e.a] = true;
        q.push(e.a);
      }
    }
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto u : g[v])
        if (!cur[u]) {
          cur[u] = true;
          q.push(u);
        }
    }
    for (auto e : kv.second) {
      g[e.a].clear();
      used[e.a] = false;
    }
    if (cur[n - 1]) {
      ans = kv.first;
      cur[n - 1] = false;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

int read() {
  int i;
  scanf("%d", &i);
  return i;
}

struct Edge {
  int a, b;
};

bool cur[114514];
vi g[114514];

int main() {
  int n = read(), m = read();
  map<int, vector<Edge>> es;
  REP(i, m) {
    int a = read() - 1, b = read() - 1, t = read();
    es[t].PB(Edge{a, b});
  }
  int ans = -1;
  cur[0] = true;
  for (auto kv : es) {
    queue<int> q;
    for (auto e : kv.second) {
      g[e.a].PB(e.b);
      if (cur[e.a])
        q.push(e.a);
    }
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto u : g[v])
        if (!cur[u]) {
          cur[u] = true;
          q.push(u);
        }
    }
    for (auto e : kv.second) {
      g[e.a].clear();
    }
    if (cur[n - 1]) {
      ans = kv.first;
      cur[n - 1] = false;
    }
  }
  cout << ans << endl;
}