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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int n, m;
  while (cin >> n >> m, n || m) {
    int f, g;
    cin >> f >> g;
    f--;
    g--;
    int a[30][30], b[30][30];
    memset(a, -1, sizeof(a));
    for (i = 0; i < m; i++) {
      int p, q, r, s;
      cin >> p >> q >> r >> s;
      p--;
      q--;
      a[p][q] = a[q][p] = r;
      b[p][q] = b[q][p] = s;
    }
    int c[31][30][30];
    memset(c, -1, sizeof(c));
    priority_queue<pair<pair<double, int>, pair<int, int>>,
                   vector<pair<pair<double, int>, pair<int, int>>>,
                   greater<pair<pair<double, int>, pair<int, int>>>>
        d;
    for (i = 0; i < n; i++) {
      if (a[f][i] != -1)
        d.push(make_pair(make_pair(a[f][i], 1), make_pair(f, i)));
    }
    while (d.empty() == 0) {
      double p;
      int q, r, s;
      p = d.top().first.first;
      q = d.top().first.second;
      r = d.top().second.first;
      s = d.top().second.second;
      if (q == 1 && s == g)
        break;
      d.pop();
      if (c[q][r][s]) {
        c[q][r][s] = 0;
        for (i = 0; i < n; i++) {
          if (i != r && a[s][i] != -1) {
            for (j = -1; j <= 1; j++) {
              if (1 <= q + j && q + j <= b[s][i] && c[q + j][s][i])
                d.push(
                    make_pair(make_pair(p + (double)a[s][i] / (q + j), q + j),
                              make_pair(s, i)));
            }
          }
        }
      }
    }
    if (d.empty())
      cout << "unreachable" << endl;
    else
      cout << d.top().first.first << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int n, m;
  while (cin >> n >> m, n || m) {
    int f, g;
    cin >> f >> g;
    f--;
    g--;
    int a[30][30], b[30][30];
    memset(a, -1, sizeof(a));
    for (i = 0; i < m; i++) {
      int p, q, r, s;
      cin >> p >> q >> r >> s;
      p--;
      q--;
      a[p][q] = a[q][p] = r;
      b[p][q] = b[q][p] = s;
    }
    int c[31][30][30];
    memset(c, -1, sizeof(c));
    priority_queue<pair<pair<double, int>, pair<int, int>>,
                   vector<pair<pair<double, int>, pair<int, int>>>,
                   greater<pair<pair<double, int>, pair<int, int>>>>
        d;
    for (i = 0; i < n; i++) {
      if (a[f][i] != -1)
        d.push(make_pair(make_pair(a[f][i], 1), make_pair(f, i)));
    }
    while (d.empty() == 0) {
      double p;
      int q, r, s;
      p = d.top().first.first;
      q = d.top().first.second;
      r = d.top().second.first;
      s = d.top().second.second;
      if (q == 1 && s == g)
        break;
      d.pop();
      if (c[q][r][s]) {
        c[q][r][s] = 0;
        for (i = 0; i < n; i++) {
          if (i != r && a[s][i] != -1) {
            for (j = -1; j <= 1; j++) {
              if (1 <= q + j && q + j <= b[s][i])
                d.push(
                    make_pair(make_pair(p + (double)a[s][i] / (q + j), q + j),
                              make_pair(s, i)));
            }
          }
        }
      }
    }
    if (d.empty())
      cout << "unreachable" << endl;
    else
      cout << d.top().first.first << endl;
  }
  return 0;
}