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

#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1e8)
#define EPS (1e-9)
#define MOD (1000000007)
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool b = 1;
  for (auto s : t)
    os << (exchange(b, 0) ||
                   strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20
               ? ""
               : " ")
       << s;
  return os << endl;
}

template <class E, class Q,
          typename C =
              typename remove_reference<decltype(get<0>(declval<Q &>()))>::type>
struct Dijkstra {
  C maxVal = numeric_limits<C>::max();
  int n;
  vector<vector<E>> edge;
  vector<C> distance;
  Dijkstra(int N) : n(N) {
    edge.resize(n);
    distance = decltype(distance)(n, INF);
  }
  void add(int a, E stat, bool dir = true) {
    edge[a].push_back(stat);
    if (!dir)
      return;
    swap(a, get<0>(stat));
    edge[a].push_back(stat);
  }
  C dist(int s, int g) {
    auto cmp = [](Q a, Q b) { return get<0>(a) > get<0>(b); };
    priority_queue<Q, vector<Q>, decltype(cmp)> pq(cmp);

    pq.emplace(0, s);
    while (!pq.empty()) {
      int cost, curr;
      tie(cost, curr) = pq.top();
      pq.pop();
      if (distance[curr] != INF)
        continue;
      distance[curr] = cost;
      if (curr == g)
        break;
      for (auto e : edge[curr]) {
        int next, dist;
        tie(next, dist) = e;
        if (distance[next] == INF) {
          pq.emplace(cost + dist, next);
        }
      }
    }
    return distance[g] == INF ? -1 : distance[g];
  }
};

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

  int n, m, c, S, G; // 駅の数、路線の数、会社数、出発、目的
  while (cin >> n >> m >> c >> S >> G, n + m + c + S + G) {
    vector<vector<vector<pii>>> route(c, vector<vector<pii>>(n));
    vi g(c);
    vvi cost(c, vi(100001, 0));
    rep(_, m) {
      int x, y, d, s;
      cin >> x >> y >> d >> s;
      route[s - 1][x - 1].emplace_back(y - 1, d);
      route[s - 1][y - 1].emplace_back(x - 1, d);
    }
    rep(i, c) cin >> g[i];
    rep(i, c) {
      vi p(g[i], 0), q(g[i], 0);
      range(j, 1, g[i]) cin >> p[j];
      rep(j, g[i]) cin >> q[j];
      rep(j, g[i]) {
        range(k, p[j] + 1, (g[i] == j + 1 ? 100000 : p[j + 1]) + 1) cost[i][k] =
            (k == 0 ? 0 : cost[i][k - 1] + q[j]);
      }
    }
    Dijkstra<tuple<int, int>, tuple<int, int>> ds(n);
    rep(s, c) {
      vvi w(n, vi(n, INF));
      rep(i, n) {
        w[i][i] = 0;
        for (auto e : route[s][i]) {
          w[i][e.first] = min(w[i][e.first], e.second);
        }
      }
      rep(k, n) rep(i, n) rep(j, n) if (w[i][k] != INF and w[k][j] != INF)
          w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
      rep(i, n) range(j, i + 1, n) if (w[i][j] != INF) {
        ds.add(i, make_tuple(j, cost[s][w[i][j]]));
      }
    }
    cout << ds.dist(S - 1, G - 1) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define LINF ((ll)1ll < 60)
#define INF ((int)1 << 30)
#define EPS (1e-3)
#define MOD (1000000007)
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool b = 1;
  for (auto s : t)
    os << (exchange(b, 0) ||
                   strlen(abi::__cxa_demangle(typeid(S).name(), 0, 0, 0)) > 20
               ? ""
               : " ")
       << s;
  return os << endl;
}

using D = double;
using P = complex<double>;
#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)

// 内積　dot(a,b) = |a||b|cosθ
D dot(P a, P b) { return (conj(a) * b).X; }
// 外積　cross(a,b) = |a||b|sinθ
D cross(P a, P b) { return (conj(a) * b).Y; }

// 点の進行方向
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return +1; // counter clockwise
  if (cross(b, c) < -EPS)
    return -1; // clockwise
  if (dot(b, c) < -EPS)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line or a==b
  return 0;    // a--c--b on line or a==c or b==c
}

/* 交差判定　直線・線分は縮退してはならない。接する場合は交差するとみなす。isecはintersectの略
 */

// 直線と点
bool isecLP(P a1, P a2, P b) {
  return abs(ccw(a1, a2, b)) != 1; // return EQ(cross(a2-a1, b-a1), 0); と等価
}

// 直線と直線
bool isecLL(P a1, P a2, P b1, P b2) {
  return !isecLP(a2 - a1, b2 - b1, 0) || isecLP(a1, b1, b2);
}

// 直線と線分
bool isecLS(P a1, P a2, P b1, P b2) {
  return cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < EPS;
}

// 線分と線分
bool isecSS(P a1, P a2, P b1, P b2) {
  return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
         ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0;
}

// 線分と点
bool isecSP(P a1, P a2, P b) { return !ccw(a1, a2, b); }

/* 距離　各直線・線分は縮退してはならない */

// 点pの直線aへの射影点を返す
P proj(P a1, P a2, P p) {
  return a1 + dot(a2 - a1, p - a1) / norm(a2 - a1) * (a2 - a1);
}

// 点pの直線aへの反射点を返す
P reflection(P a1, P a2, P p) { return 2.0 * proj(a1, a2, p) - p; }

D distLP(P a1, P a2, P p) { return abs(proj(a1, a2, p) - p); }

D distLL(P a1, P a2, P b1, P b2) {
  return isecLL(a1, a2, b1, b2) ? 0 : distLP(a1, a2, b1);
}

D distLS(P a1, P a2, P b1, P b2) {
  return isecLS(a1, a2, b1, b2) ? 0
                                : min(distLP(a1, a2, b1), distLP(a1, a2, b2));
}

D distSP(P a1, P a2, P p) {
  P r = proj(a1, a2, p);
  if (isecSP(a1, a2, r))
    return abs(r - p);
  return min(abs(a1 - p), abs(a2 - p));
}

D distSS(P a1, P a2, P b1, P b2) {
  if (isecSS(a1, a2, b1, b2))
    return 0;
  return min(min(distSP(a1, a2, b1), distSP(a1, a2, b2)),
             min(distSP(b1, b2, a1), distSP(b1, b2, a2)));
}

bool inner(double l, double r, double v) {
  return min(l, r) <= v and v <= max(l, r);
}

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

  int n;
  while (cin >> n, n) {
    double ax, ay, bx, by;
    cin >> ax >> ay >> bx >> by;
    P s(ax, ay), g(bx, by);

    double ans = 1e9;
    rep(i, n) {
      vector<P> r;
      int h;
      cin >> ax >> ay >> bx >> by >> h;
      r = {P(ax, ay), P(ax, by), P(bx, by), P(bx, ay)};
      double dist = 1e9;
      if (inner(ax, bx, s.X) and inner(ax, bx, g.X) and inner(ay, by, s.Y) and
          inner(ay, by, g.Y))
        dist = 0;
      rep(i, 4) {
        dist = min(dist, distSS(s, g, r[i], r[(i + 3) % 4]));
        if (isecSS(s, g, r[i], r[(i + 3) % 4]))
          dist = 0;
      }
      if (LE(dist, h))
        ans = min(ans, dist);
      else
        ans = min(ans, (h * h + dist * dist) / 2 / h);
    }
    cout.precision(4);
    cout << fixed << ans << endl;
  }
  return 0;
}