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

#include <cstdio>
#include <map>
#include <queue>
#include <vector>
using namespace std;
struct E {
  int t, c, d;
  E(int t, int c, int d) : t(t), c(c), d(d) {}
};
vector<E> e[30];
struct S {
  double t;
  int cur, last, s;
  S(double t, int cur, int last, int s) : t(t), cur(cur), last(last), s(s) {}
};
struct Cmp {
  bool operator()(const S &a, const S &b) { return a.t > b.t; }
};
double F(int s, int g) {
  double time[31][30][30];
  int i, j, k;
  fill_n(&time[0][0][0], sizeof(time) / sizeof(double), 1e99);
  priority_queue<S, vector<S>, Cmp> q;
  q.push(S(0, s, s, 1));
  while (!q.empty()) {
    S s = q.top();
    q.pop();
    if (time[s.s][s.cur][s.last] <= s.t)
      continue;
    time[s.s][s.cur][s.last] = s.t;
    if (s.cur == g && s.s == 1)
      return s.t;
    for (i = 0; i < e[s.cur].size(); ++i) {
      E &r = e[s.cur][i];
      if (s.last == r.t)
        continue;
      if (s.cur == s.last)
        q.push(S(s.t + r.d / double(s.s), r.t, s.cur, s.s));
      else
        for (j = (s.s - 1 ? -1 : 0); j < 2; ++j)
          if (s.s + j <= r.c &&
              time[s.s + j][r.t][s.cur] > s.t + r.d / double(s.s + j))
            q.push(S(s.t + r.d / double(s.s + j), r.t, s.cur, s.s + j));
    }
  }
  return -1;
}
int main() {
  int n, m, s, g, x, y, d, c, i;
  while (scanf("%d%d", &n, &m), n) {
    for (i = 0; i < n; ++i)
      e[i].clear();
    scanf("%d%d", &s, &g);
    while (m--) {
      scanf("%d%d%d%d", &x, &y, &d, &c);
      --x, --y;
      e[x].push_back(E(y, c, d));
      e[y].push_back(E(x, c, d));
    }
    double r = F(s - 1, g - 1);
    if (r < 0)
      puts("unreachable");
    else
      printf("%f\\n", r);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <map>
#include <queue>
#include <vector>
using namespace std;
struct E {
  int t, c, d;
  E(int t, int c, int d) : t(t), c(c), d(d) {}
};
vector<E> e[30];
struct S {
  double t;
  int cur, last, s;
  S(double t, int cur, int last, int s) : t(t), cur(cur), last(last), s(s) {}
};
struct Cmp {
  bool operator()(const S &a, const S &b) { return a.t > b.t; }
};
double F(int s, int g) {
  double time[31][30][30];
  int i, j, k;
  fill_n(&time[0][0][0], sizeof(time) / sizeof(double), 1e99);
  priority_queue<S, vector<S>, Cmp> q;
  q.push(S(0, s, s, 1));
  while (!q.empty()) {
    S s = q.top();
    q.pop();
    if (time[s.s][s.cur][s.last] <= s.t)
      continue;
    time[s.s][s.cur][s.last] = s.t;
    if (s.cur == g && s.s == 1)
      return s.t;
    for (i = 0; i < e[s.cur].size(); ++i) {
      E &r = e[s.cur][i];
      if (s.last == r.t)
        continue;
      if (s.cur == s.last)
        q.push(S(s.t + r.d / double(s.s), r.t, s.cur, s.s));
      else
        for (j = (s.s - 1 ? -1 : 0); j < 2; ++j)
          if (s.s + j <= r.c)
            q.push(S(s.t + r.d / double(s.s + j), r.t, s.cur, s.s + j));
    }
  }
  return -1;
}
int main() {
  int n, m, s, g, x, y, d, c, i;
  while (scanf("%d%d", &n, &m), n) {
    for (i = 0; i < n; ++i)
      e[i].clear();
    scanf("%d%d", &s, &g);
    while (m--) {
      scanf("%d%d%d%d", &x, &y, &d, &c);
      --x, --y;
      e[x].push_back(E(y, c, d));
      e[y].push_back(E(x, c, d));
    }
    double r = F(s - 1, g - 1);
    if (r < 0)
      puts("unreachable");
    else
      printf("%f\\n", r);
  }
  return 0;
}