Problem p01911 (C++) — buggy submission, verdict: Time Limit Exceeded

/* attention to overflow */
#include <bits/stdc++.h>

#define dump(x) cerr << #x << " = " << x << endl
#define ALL(container) (container).begin(), (container).end()

using namespace std;
const int INF = 1 << 25;
void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &s) {
  return os << '(' << s.first << ',' << s.second << ')';
}
/*printf("%.9Lf\\n",cf);*/
const int MOD = 1000000007;
const double EPS = 1e-8;

int dp[200010];
struct ed {
  int u, v, t, c;
};
vector<ed> eds(200010);
struct edge {
  int no, u, tc;
};
vector<edge> Graph[100010];

int N, M, S, G;

int memo(int q) {
  if (dp[q] != -1)
    return dp[q];

  int uofq = eds[q].u;
  int tofq = eds[q].t;

  if (uofq == S)
    return dp[q] = tofq;

  int tmp = INF;
  for (int i = 0; i < Graph[uofq].size(); i++) {
    int no = Graph[uofq][i].no;
    int tc = Graph[uofq][i].tc;
    // if(tc>tofq) continue;
    if (tc > tofq)
      break;
    tmp = min(tmp, (tofq - tc) + memo(no));
  }
  return dp[q] = tmp;
}

int main() {
  io();
  cin >> N >> M >> S >> G;
  S--, G--;
  fill_n(dp, M + 10, -1);
  for (int i = 0; i < M; i++) {
    int u, v, t, c;
    cin >> u >> v >> t >> c;
    u--, v--;
    Graph[v].push_back({i, u, t + c});

    // eds.push_back({u,v,t,c});
    eds[i] = {u, v, t, c};
  }

  for (int i = 0; i < N; i++) {
    sort(Graph[i].begin(), Graph[i].end(),
         [](const edge &a, const edge &b) { return a.tc < b.tc; });
  }

  int ans = INF;
  for (int i = 0; i < M; i++) {
    if (eds[i].v == G)
      ans = min(ans, memo(i));
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

/* attention to overflow */
#include <bits/stdc++.h>

#define dump(x) cerr << #x << " = " << x << endl
#define ALL(container) (container).begin(), (container).end()

using namespace std;
const int INF = 1 << 25;
void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &s) {
  return os << '(' << s.first << ',' << s.second << ')';
}
/*printf("%.9Lf\\n",cf);*/
const int MOD = 1000000007;
const double EPS = 1e-8;

int dp[200010];
struct ed {
  int u, v, t, c;
};
vector<ed> eds(200010);
struct edge {
  int no, u, tc;
};
vector<edge> Graph[100010];

int N, M, S, G;

int memo(int q) {
  if (dp[q] != -1)
    return dp[q];

  int uofq = eds[q].u;
  int tofq = eds[q].t;

  if (uofq == S)
    return dp[q] = tofq;

  int tmp = INF;
  for (int i = 0; i < Graph[uofq].size(); i++) {
    int no = Graph[uofq][i].no;
    int tc = Graph[uofq][i].tc;
    if (tc > tofq)
      continue;
    // if(tc>tofq) break;
    tmp = min(tmp, (tofq - tc) + memo(no));
  }
  return dp[q] = tmp;
}

int main() {
  io();
  cin >> N >> M >> S >> G;
  S--, G--;
  fill_n(dp, M + 10, -1);
  for (int i = 0; i < M; i++) {
    int u, v, t, c;
    cin >> u >> v >> t >> c;
    u--, v--;
    Graph[v].push_back({i, u, t + c});

    // eds.push_back({u,v,t,c});
    eds[i] = {u, v, t, c};
  }

  /*for(int i=0;i<N;i++){
          sort(Graph[i].begin(),Graph[i].end(),[](const edge &a, const edge
  &b){return a.tc<b.tc;});
  }*/

  int ans = INF;
  for (int i = 0; i < M; i++) {
    if (eds[i].v == G)
      ans = min(ans, memo(i));
  }
  cout << ans << endl;

  return 0;
}