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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int d, i, o;
  Edge(int d, int i, int o) : d(d), i(i), o(o) {}
};

class State {
public:
  int n, t;
  State(int n, int t) : n(n), t(t) {}

  bool operator<(const State &s) const { return t > s.t; }
};

typedef vector<Edge> Edges;
typedef pair<vector<int>, vector<int>> Train;

int input() {
  string s;
  cin >> s;

  return ((s[0] - '0') * 10 + (s[1] - '0')) * 60 +
         ((s[3] - '0') * 10 + (s[4] - '0'));
}

int beg[1000], end[1000];

int dijk(int S, int st, int lim, vector<Edges> &eg, bool f) {
  bool v[1000] = {0};
  priority_queue<State> q;
  q.push(State(S, st));

  while (!q.empty()) {
    State s = q.top();
    q.pop();
    if (s.t > lim)
      break;

    if (v[s.n])
      continue;
    v[s.n] = 1;

    if (f)
      beg[s.n] = s.t;
    else
      end[s.n] = -s.t + 24 * 60;

    for (int i = 0; i < eg[s.n].size(); i++) {
      Edge e = eg[s.n][i];
      if (v[e.d])
        continue;
      if (s.t > e.i)
        continue;

      q.push(State(e.d, e.o));
    }
  }

  return -1;
}

int main() {
  int S, T;
  while (cin >> S >> T, (S || T)) {
    int src, dst, st, dt;
    cin >> src;
    st = input();
    cin >> dst;
    dt = input();

    src--;
    dst--;

    vector<Edges> eg(1000), reg(1000);
    vector<Train> trains(100);

    for (int i = 0; i < T; i++) {
      int N;
      cin >> N;
      vector<int> p, t;
      for (int j = 0; j < N; j++) {
        int a, b;
        cin >> a;
        b = input();
        a--;

        p.push_back(a);
        t.push_back(b);
      }
      trains[i] = make_pair(p, t);

      for (int j = 0; j < N - 1; j++) {
        int s = p[j], d = p[j + 1];
        int a = t[j], b = t[j + 1];
        eg[s].push_back(Edge(d, a, b));
        reg[d].push_back(Edge(s, 24 * 60 - b, 24 * 60 - a));
      }
    }

    for (int i = 0; i < S; i++) {
      beg[i] = 9999;
      end[i] = -1;
    }

    dijk(src, st, dt, eg, 1);
    dijk(dst, 24 * 60 - dt, 24 * 60 - st, reg, 0);

    int ret = -1;
    if (src == dst && st <= dt)
      ret = 0;

    for (int i = 0; i < T; i++) {
      int lo = 9999, hi = -1;
      for (int j = 0; j < trains[i].first.size(); j++) {
        int p = trains[i].first[j], t = trains[i].second[j];
        if (beg[p] <= t)
          lo = min(j, lo);
        if (end[p] >= t)
          hi = max(j, hi);
      }

      if (lo == 9999 || hi == -1)
        continue;
      ret = max(ret, trains[i].second[hi] - trains[i].second[lo]);
    }
    if (ret == -1)
      cout << "impossible" << endl;
    else
      cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int d, i, o;
  Edge(int d, int i, int o) : d(d), i(i), o(o) {}
};

class State {
public:
  int n, t;
  State(int n, int t) : n(n), t(t) {}

  bool operator<(const State &s) const { return t > s.t; }
};

typedef vector<Edge> Edges;

int input() {
  string s;
  cin >> s;

  return ((s[0] - '0') * 10 + (s[1] - '0')) * 60 +
         ((s[3] - '0') * 10 + (s[4] - '0'));
}

int dijk(int S, int T, int st, int lim, vector<Edges> &eg) {
  bool v[1000] = {0};
  priority_queue<State> q;
  q.push(State(S, st));

  while (!q.empty()) {
    State s = q.top();
    q.pop();
    if (s.t > lim)
      break;

    if (v[s.n])
      continue;
    v[s.n] = 1;

    if (s.n == T) {
      return s.t;
    }

    for (int i = 0; i < eg[s.n].size(); i++) {
      Edge e = eg[s.n][i];
      if (v[e.d])
        continue;
      if (s.t > e.i)
        continue;

      q.push(State(e.d, e.o));
    }
  }

  return -1;
}

int main() {
  int S, T;
  while (cin >> S >> T, (S || T)) {
    int src, dst, st, dt;
    cin >> src;
    st = input();
    cin >> dst;
    dt = input();

    src--;
    dst--;

    vector<Edges> eg(1000);

    for (int i = 0; i < T; i++) {
      int N;
      cin >> N;
      vector<int> p, t;
      for (int j = 0; j < N; j++) {
        int a, b;
        cin >> a;
        b = input();
        a--;

        p.push_back(a);
        t.push_back(b);
      }

      for (int j = 0; j < N; j++)
        for (int k = j + 1; k < N; k++) {
          int s = p[j], d = p[k];
          int a = t[j], b = t[k];
          eg[s].push_back(Edge(d, a, b));
        }
    }

    int beg[1000], end[1000] = {0};
    for (int i = 0; i < S; i++) {
      beg[i] = dijk(src, i, st, dt, eg);
      for (int j = 0; j < eg[i].size(); j++) {
        if (dijk(eg[i][j].d, dst, eg[i][j].o, dt, eg) == -1)
          continue;
        end[eg[i][j].d] = max(end[i], eg[i][j].o);
      }
    }

    int ret = -1;
    if (src == dst && st <= dt)
      ret = 0;

    for (int i = 0; i < S; i++)
      for (int j = 0; j < eg[i].size(); j++) {
        Edge e = eg[i][j];
        if (e.i < beg[i] || e.o > end[e.d])
          continue;
        ret = max(ret, e.o - e.i);
      }

    if (ret == -1)
      cout << "impossible" << endl;
    else
      cout << ret << endl;
  }
}