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

#include "bits/stdc++.h"

#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
struct Edge {
  int src;
  int dst;
  long long int A;
  long long int B;
};
struct aa {
  int now;
  long long int cost;
};
bool operator<(const aa &l, const aa &r) { return l.cost > r.cost; }
long long int ce(long long int a, long long int b) { return (a - 1) / b + 1; }
int main() {
  int N, M, S, G;
  cin >> N >> M >> S >> G;
  S--;
  G--;
  vector<vector<Edge>> g(N);

  for (int i = 0; i < M; ++i) {
    int a, b;
    long long int A, B;
    cin >> a >> b >> A >> B;
    a--;
    b--;
    g[a].push_back(Edge{a, b, A, B});
    g[b].push_back(Edge{b, a, A, B});
  }

  vector<long long int> memo(N, 1e18);
  memo[S] = 0;
  priority_queue<aa> que;
  que.push(aa{S, 0});
  while (!que.empty()) {
    auto atop(que.top());
    que.pop();
    const int now = atop.now;
    const long long int now_cost = atop.cost;
    if (memo[now] != now_cost)
      continue;
    for (auto e : g[now]) {
      int next = e.dst;
      long long int next_cost = now_cost;
      if (e.A >= 1e8) {
        next_cost = now_cost + ce(e.B, e.A + now_cost);
      } else {
        long long int needt = sqrt(e.B) - e.A;

        long long int this_cost = 1e18;
        for (long long int stop_t = needt - 1; stop_t <= needt + 1; ++stop_t) {
          long long int at = max(stop_t, now_cost);
          { this_cost = min(this_cost, at + ce(e.B, e.A + at)); }
        }
        next_cost = this_cost;
      }
      if (memo[next] > next_cost) {
        memo[next] = next_cost;
        que.push(aa{next, next_cost});
      }
    }
  }
  if (memo[G] > 7e17)
    memo[G] = -1;
  cout << memo[G] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
struct Edge {
  int src;
  int dst;
  long long int A;
  long long int B;
};
struct aa {
  int now;
  long long int cost;
};
bool operator<(const aa &l, const aa &r) { return l.cost > r.cost; }
long long int ce(long long int a, long long int b) { return (a - 1) / b + 1; }
int main() {
  int N, M, S, G;
  cin >> N >> M >> S >> G;
  S--;
  G--;
  vector<vector<Edge>> g(N);

  for (int i = 0; i < M; ++i) {
    int a, b;
    long long int A, B;
    cin >> a >> b >> A >> B;
    a--;
    b--;
    g[a].push_back(Edge{a, b, A, B});
    g[b].push_back(Edge{b, a, A, B});
  }

  vector<long long int> memo(N, 1e18);
  memo[S] = 0;
  priority_queue<aa> que;
  que.push(aa{S, 0});
  while (!que.empty()) {
    auto atop(que.top());
    que.pop();
    const int now = atop.now;
    const long long int now_cost = atop.cost;
    for (auto e : g[now]) {
      int next = e.dst;
      long long int next_cost = now_cost;
      if (e.A >= 1e8) {
        next_cost = now_cost + ce(e.B, e.A + now_cost);
      } else {
        long long int needt = sqrt(e.B) - e.A;

        long long int this_cost = 1e18;
        for (long long int stop_t = needt - 2; stop_t <= needt + 2; ++stop_t) {
          long long int at = max(stop_t, now_cost);
          { this_cost = min(this_cost, at + ce(e.B, e.A + at)); }
        }
        next_cost = this_cost;
      }
      if (memo[next] > next_cost) {
        memo[next] = next_cost;
        que.push(aa{next, next_cost});
      }
    }
  }
  if (memo[G] > 7e17)
    memo[G] = -1;
  cout << memo[G] << endl;
  return 0;
}