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

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

struct edge {
  int to, cost, limit;
  edge(int t, int c, int l) {
    to = t;
    cost = c;
    limit = l;
  }
};

#define INF 1000000

struct Node {
  int now, speed, before;
  double time;
  Node(int n, int s, double t, int bef) {
    now = n;
    speed = s;
    time = t;
    before = bef;
  }
};

bool operator<(Node a, Node b) { return a.time > b.time; }

int n, m, s, g;

void solve() {
  bool visit[31][31][31];
  memset(visit, false, sizeof(visit));
  vector<vector<edge>> E(n + 1);
  cin >> s >> g;
  for (int i = 0; i < m; i++) {
    int in, out, c, l;
    cin >> in >> out >> c >> l;
    E[in].push_back(edge(out, c, l));
    E[out].push_back(edge(in, c, l));
  }

  priority_queue<Node> Q;
  Q.push(Node(s, 0, 0.0, s));
  while (!Q.empty()) {
    Node t = Q.top();
    Q.pop();
    if (visit[t.now][t.before][t.speed])
      continue;
    visit[t.now][t.before][t.speed] = true;
    if (t.now == g && t.speed == 1) {
      printf("%.12lf\\n", t.time);
      return;
    }

    int size = E[t.now].size();
    for (int i = 0; i < size; i++) {
      int to = E[t.now][i].to;
      if (to == t.before)
        continue;
      if (E[t.now][i].cost > 0) {
        if (t.speed >= 1 && E[t.now][i].limit >= t.speed) {
          Q.push(Node(to, t.speed, t.time + E[t.now][i].cost / (double)t.speed,
                      t.now));
        }

        if (E[t.now][i].limit >= t.speed + 1 && !visit[to][t.now][t.speed + 1])
          Q.push(Node(to, t.speed + 1,
                      t.time + E[t.now][i].cost / (double)(t.speed + 1.0),
                      t.now));

        if (t.speed - 1 >= 1 && !visit[to][t.now][t.speed - 1] &&
            E[t.now][i].limit >= t.speed - 1)
          Q.push(Node(to, t.speed - 1,
                      t.time + E[t.now][i].cost / (double)(t.speed - 1.0),
                      t.now));
      }
    }
  }
  cout << "unreachable" << endl;
}

int main() {
  while (cin >> n >> m && n != 0)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

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

struct edge {
  int to, cost, limit;
  edge(int t, int c, int l) {
    to = t;
    cost = c;
    limit = l;
  }
};

#define INF 1000000

struct Node {
  int now, speed, before;
  double time;
  Node(int n, int s, double t, int bef) {
    now = n;
    speed = s;
    time = t;
    before = bef;
  }
};

bool operator<(Node a, Node b) { return a.time > b.time; }

int n, m, s, g;

void solve() {
  bool visit[31][31][31];
  memset(visit, false, sizeof(visit));
  vector<vector<edge>> E(n + 1);
  cin >> s >> g;
  for (int i = 0; i < m; i++) {
    int in, out, c, l;
    cin >> in >> out >> c >> l;
    E[in].push_back(edge(out, c, l));
    E[out].push_back(edge(in, c, l));
  }

  priority_queue<Node> Q;
  Q.push(Node(s, 0, 0.0, s));
  while (!Q.empty()) {
    Node t = Q.top();
    Q.pop();
    if (visit[t.now][t.before][t.speed])
      continue;
    visit[t.now][t.before][t.speed] = true;
    if (t.now == g && t.speed == 1) {
      printf("%.12lf\\n", t.time);
      return;
    }

    int size = E[t.now].size();
    for (int i = 0; i < size; i++) {
      int to = E[t.now][i].to;
      if (to == t.before)
        continue;
      if (E[t.now][i].cost > 0) {
        if (E[t.now][i].limit >= t.speed && t.speed >= 1) {
          Q.push(Node(to, t.speed, t.time + E[t.now][i].cost / (double)t.speed,
                      t.now));
        }

        if (E[t.now][i].limit >= t.speed + 1)
          Q.push(Node(to, t.speed + 1,
                      t.time + E[t.now][i].cost / (double)(t.speed + 1.0),
                      t.now));

        if (E[t.now][i].limit >= t.speed - 1 && t.speed - 1 >= 1)
          Q.push(Node(to, t.speed - 1,
                      t.time + E[t.now][i].cost / (double)(t.speed - 1.0),
                      t.now));
      }
    }
  }
  cout << "unreachable" << endl;
}

int main() {
  while (cin >> n >> m && n != 0)
    solve();
  return 0;
}